Microsoft Graph
一种 Microsoft 可编程性模型,用于公开 REST API 和客户端库以访问 Microsoft 365 服务上的数据。
93 个问题
我的代码如下
<?php
use Microsoft\Kiota\Authentication\Oauth\ClientCredentialContext;
use Microsoft\Graph\GraphServiceClient;
use Microsoft\Graph\Generated\Users\Item\Messages\MessagesRequestBuilderGetRequestConfiguration;
public function read(){
$tokenContext = new ClientCredentialContext(
$this->tenantId,
$this->clientId,
$this->secret
);
$graphServiceClient = new GraphServiceClient($tokenContext, ['.default']);
$requestConfiguration = new MessagesRequestBuilderGetRequestConfiguration();
$queryParameters = MessagesRequestBuilderGetRequestConfiguration::createQueryParameters();
$queryParameters->top = 100;
$requestConfiguration->queryParameters = $queryParameters;
$requestConfiguration->headers = ['Prefer' => 'outlook.body-content-type="text"',];
$messagesPage = $graphServiceClient->users()->byUserId($this->userEmail)->messages()->get($requestConfiguration)->wait();
// 分页循环
while ($messagesPage !== null && $messagesPage->getValue() !== null) {
$nextLink = $messagesPage->getOdataNextLink();
if ($nextLink) {
$messagesPage = $graphServiceClient
->users()
->byUserId($this->userEmail)
/*->mailFolders()
->byMailFolderId('inbox')*/
->messages()
->withUrl($nextLink)
->get($requestConfiguration)
->wait();
} else {
break;
}
}
$messages = $messagesPage->getValue();
foreach ($messages as $message) {
$arr[] = [
'id' => $message->getId(),
'subject' => $message->getSubject(),
'from' => $message->getSender()?->getEmailAddress()?->getAddress(),
'receivedDateTime' => $message->getReceivedDateTime()?->format('Y-m-d H:i:s'),
];
}
dump($arr);
return $arr;
}
我打印的结果如下:
array:2 [
0 => array:4 [
"id" => "AAMkADE5NzA3NGVjLTRhOTktNGJiNi05NDBkLTNmY2I3MjJjODQ0YgBGAAAAAAAUxyvfIOcNRYSlwCLzBzrzBwCqgX5V0qwgT6yFLqVqW-JpAAAAAAEMAACqgX5V0qwgT6yFLqVqW-JpAAATu848AAA="
"subject" => "View your Exchange Online Kiosk invoice"
"from" => "microsoft-noreply@microsoft.com"
"receivedDateTime" => "2025-04-13 03:21:37"
``` ]
1 => array:4 [
```scala
"id" => "AAMkADE5NzA3NGVjLTRhOTktNGJiNi05NDBkLTNmY2I3MjJjODQ0YgBGAAAAAAAUxyvfIOcNRYSlwCLzBzrzBwCqgX5V0qwgT6yFLqVqW-JpAAAAAAEKAACqgX5V0qwgT6yFLqVqW-JpAAAAAQ0aAAA="
"subject" => ""
"from" => null
"receivedDateTime" => "2025-03-13 06:21:51"
``` ]
]
可以看到我只能拿到两封邮件,其中一封邮件的主题都不存在, 实际我的邮箱中的有6封邮件,是我的传参有问题还是我的配置有问题吗?或者是我配置的权限有问题吗?