我正在使用( https://github.com/microsoftgraph/msgraph-sdk-php )Microsoft graph API
从 Microsoft 帐户中检索我的消息。php SDK
我的代码示例如下
<?php
// Autoload files using the Composer autoloader.
require_once __DIR__ . '/vendor/autoload.php';
use Microsoft\Graph\Graph;
use Microsoft\Graph\Model;
//get the access token to access graph api
$tenantId = "XXXXXX";
$clientId = "XXXXXXXXXXXX";
$clientSecret = "XXXXXXXXXXX";
$guzzleClient = new \GuzzleHttp\Client(array('curl' => array( CURLOPT_SSL_VERIFYPEER => false)));
$url = 'https://login.microsoftonline.com/' . $tenantId . '/oauth2/token?api-version=1.0';
$token = json_decode($guzzleClient->post($url, [
'form_params' => [
'client_id' => $clientId,
'client_secret' => $clientSecret,
'resource' => 'https://graph.microsoft.com/',
'grant_type' => 'client_credentials',
],
])->getBody()->getContents());
$accessToken = $token->access_token;
//get the messages of user
$graph = new Graph();
$graph->setAccessToken($accessToken);
$messages = $graph->createRequest("GET", "/me/messages")
->setReturnType(Model\User::class)
->execute();
print_r($messages); exit;
但它会引发错误,如下所示:
致命错误:未捕获的 GuzzleHttp\Exception\ClientException:客户端错误:
GET https://graph.microsoft.com/v1.0/me/messages
导致400 Bad Request
响应:{“错误”:{“代码”:“BadRequest”,“消息”:“当前经过身份验证的上下文对此请求无效。(截断。 ..) 在第 113 行的 C:\wamp64\www\graph_api\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php
这是因为访问 Graph API 的任何权限问题吗?我在Microsoft app registration portal
以及在 azure 门户中
什么可能导致此问题?有什么办法可以解决问题吗?