【问题标题】:TheNetworg oAuth2 to Microsoft Graph reports token expired while it's notTheNetworg oAuth2 to Microsoft Graph 报告令牌已过期,但未过期
【发布时间】: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?它会在“声明”选项卡中告诉您令牌何时过期。你的服务器时钟可以关闭吗?

标签: php azure oauth-2.0


【解决方案1】:

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

添加此行解决了问题:

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

【讨论】:

    猜你喜欢
    • 2016-02-15
    • 1970-01-01
    • 2021-05-09
    • 1970-01-01
    • 1970-01-01
    • 2017-12-02
    • 2020-05-19
    • 2014-06-27
    • 2019-12-11
    相关资源
    最近更新 更多