簡體   English   中英

獲得OAuth access_token后無法訪問Microsoft Graph API

[英]Unable to access Microsoft Graph API after getting OAuth access_token

我正在使用adal.js通過Microsoft OAuth生成訪問令牌,但是每當我嘗試使用訪問令牌來調用https://graph.microsoft.com/v1.0/me端點(或graph.windows)時,我都會使用它。凈),我收到以下錯誤消息: Authentication_MissingOrMalformed:缺少訪問令牌或格式錯誤。

關於如何解決此問題的任何想法? 這是我在JS中的配置:

<script src="https://secure.aadcdn.microsoftonline-p.com/lib/1.0.12/js/adal.min.js"></script>
<script>
    var authContext = new AuthenticationContext({
      instance: 'https://login.microsoft.com/',     
      tenant: 'xxxxxx-xxxxxxx-xxxxxx-xxxxxx', //COMMON OR YOUR TENANT ID
      clientId: 'xxxxxx-xxxxxxx-xxxxxx-xxxxxx', //REPLACE WITH YOUR CLIENT ID
      redirectUri: '/login.php', //REPLACE WITH YOUR REDIRECT URL
      callback: getUser,
      popUp: true,
      cacheLocation: 'localStorage'
  });

    ...

    authContext.login();
    // SET COOKIE
    var newToken = authContext.getCachedToken('tenantid-xxxxxxx-xxxxxx-xxxxxx');
    var now = new Date();
    now.setTime(now.getTime() + 1 * 3600 * 1000);
    document.cookie = "token="+newToken+"; expires=" + now.toUTCString() + "; path=/";
</script>

這就是我嘗試在我的PHP腳本中提取/使用令牌的方式:

<?php
// Get the token
$token = $_COOKIE['token'];

// Set headers
$headers = array(
    "Authorization: Bearer " . $token,
    'Content-Type: application/json'
);

// Make request to Graph API
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://graph.windows.net/mywebsite.org/me?api-version=1.6");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response  = curl_exec($ch);
$response = json_decode($response);
curl_close($ch);

echo "<pre>";
var_dump($response);
echo "</pre>";
?>

它所做的只是返回此錯誤: Authentication_MissingOrMalformed:訪問令牌丟失或格式錯誤。

我怎樣才能解決這個問題?? 沒有指定正確的資源是否有問題?

若要成功調用Azure AD Graph REST,我們需要獲取Azure AD Graph的令牌。

要檢查令牌對於Azure AD圖是否正確,可以打印令牌並從此處解析它。

令牌中的aud聲明應為https://graph.windows.net 如果不匹配,則需要使用acquireToken來獲取令牌,而不是從緩存中獲取令牌。 並且resource參數應為https://graph.windows.net

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM