TheNetworg oAuth2 to Microsoft Graph 报告令牌已过期,而未过期



我正在尝试构建一个简单的MSGraph API调用来熟悉Graph。然而,我无法让它发挥作用。MS Graph不断给出我的令牌已过期的错误,而事实并非如此。

代码:

<?php
require_once('C:inetpubsite6vendorautoload.php');
// Using newest version of TheNetworg Oauth2
$provider = new TheNetworgOAuth2ClientProviderAzure([
'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 = TheNetworgOAuth2ClientProviderAzure::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 \Auth2\Client\Provider\Exception\IdentityProviderException:您的访问令牌已过期。请在提交之前续订要求在里面C: \inetpub\site6\vendor\thentworg\oauth2 azure\src\Provider\azure.php:394

我做错了什么?当我在谷歌上搜索错误时,我会得到很多结果,告诉我我正试图使用Azure AD Graph令牌访问MS Graph,但当我回显$baseGraphUri时;我真的告诉我graph.microsoft.com。

我发现了错误是什么。尽管我使用了"baseGraphUri=$provider->getRootMicrosoftGraphUri(null(">,库仍然连接到Azure AD API,而不是Microsoft Graph API。因此,它使用错误类型(aud(的令牌进行身份验证。

添加这行修复了问题:

$provider->urlAPI = 'https://graph.microsoft.com/';

最新更新