【发布时间】:2020-11-28 04:23:06
【问题描述】:
我正在尝试构建一个简单的 MS Graph API 调用来熟悉 Graph。 但是,我无法让它工作。 MS Graph 不断给出我的令牌已过期的错误,而事实并非如此。
代码:
<?php
require_once('C:\inetpub\site6\vendor\autoload.php');
// Using newest version of TheNetworg Oauth2
$provider = new TheNetworg\OAuth2\Client\Provider\Azure([
'clientId' => '***************',
'clientSecret' => '**********',
'redirectUri' => 'https://app2.***/test.php'
]);
// Set to use v2 API, skip the line or set the value to Azure::ENDPOINT_VERSION_1_0 if willing to use v1 API
$provider->defaultEndPointVersion = TheNetworg\OAuth2\Client\Provider\Azure::ENDPOINT_VERSION_2_0;
$baseGraphUri = $provider->getRootMicrosoftGraphUri(null);
//echo $baseGraphUri;
$provider->tenant = '*********.onmicrosoft.com'; //Azure AD ID
$provider->authWithResource;
$provider->scope = $baseGraphUri . '/.default';
$token = $provider->getAccessToken('client_credentials', ['scope' => $provider->scope]);
// echo $token;
// Set up our request to the API
$ref= 'users/someuser@mytenant.com';
$response = $provider->get($ref, $token, $headers = []);
// Store the result as an object
$result = json_decode( $response->getBody() );
?>
但我总是遇到错误:
PHP 致命错误:未捕获 League\OAuth2\Client\Provider\Exception\IdentityProviderException: 您的访问令牌已过期。请在提交之前更新它 要求。在 C:\inetpub\site6\vendor\thenetworg\oauth2-azure\src\Provider\Azure.php:394
我做错了什么?当我用谷歌搜索错误时,我得到很多结果,告诉我我正在尝试使用 Azure AD Graph 令牌访问 MS Graph,但是当我执行 echo $baseGraphUri;我真的告诉我 graph.microsoft.com。
【问题讨论】:
-
你能在例如检查令牌吗? jwt.ms?它会在“声明”选项卡中告诉您令牌何时过期。你的服务器时钟可以关闭吗?