【发布时间】:2019-02-08 07:17:58
【问题描述】:
我正在使用Microsoft graph API 使用php SDK (https://github.com/microsoftgraph/msgraph-sdk-php) 从Microsoft 帐户检索我的消息。
我的代码示例如下
<?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 门户中
什么可能导致这个问题?有什么办法可以解决这个问题?
【问题讨论】:
标签: php api microsoft-graph-api microsoft-graph-sdks