![](/img/trans.png)
[英]Microsoft Graph API: Access token validation failure. Invalid audience
[英]Microsoft Graph API access token validation failure
我使用這個 URL 來獲取 id_token:
https://login.microsoftonline.com/common/oauth2/authorize?
response_type=id_token%20code&
client_id=MY_CLIENT_GUID_ID_IN_HERE&
redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fauth%2Fopenid%2Freturn&nonce=alfaYYCTxBK8oypM&
state=6DnAi0%2FICAWaH14e
這個返回結果是這樣的
http://localhost:3000/auth/openid/return?
code=AAA_code_in_here&
id_token=eyJ0eXAi_xxxx_yyyy_in_here&
state=6DnAi0%2FICAWaH14e&
session_state=xxxx_guid_xxxxx
然后我使用id_token來查詢 Graph(使用 POST man)
我已經看到這篇文章InvalidAuthenticationToken 和 CompactToken 問題 - Microsoft Graph using PHP Curl但沒有意義。
OATH 2.0 需要多個步驟。 第一個請求返回一個 OAUTH 代碼。 下一步是將該 OATUH 代碼轉換為承載令牌。 這是您在這里缺少的步驟。
我還建議使用v2 Endpoint ,它更容易使用(尤其是使用 Graph)。 我寫了一個v2 Endpoint Primer來介紹整個過程,可能也會有所幫助。
您不能直接使用令牌,還有一個步驟可以將您從響應 url 獲得的代碼交換為令牌。
這是我的 C# 代碼(使用 Microsoft.IdentityModel.Clients.ActiveDirectory)
public static AuthenticationResult ExchangeCodeForToken(string InTenantName, string InUserObjId, string InRedirectUri, string InApplicationAzureClientID, string InApplicationAzureClientAppKey)
{
Check.Require(!string.IsNullOrEmpty(InTenantName), "InTenantName must be provided");
Check.Require(!string.IsNullOrEmpty(InUserObjId), "InUserObjId must be provided");
if (CanCompleteSignIn) //redirect from sign-in
{
var clientCredential = new ClientCredential(InApplicationAzureClientID, InApplicationAzureClientAppKey);
var authContext = new AuthenticationContext(Globals.GetLoginAuthority(InTenantName), (TokenCache)new ADALTokenCache(InUserObjId)); //Login Authority is https://login.microsoftonline.com/TenantName
return authContext.AcquireTokenByAuthorizationCode(VerificationCode, new Uri(InRedirectUri), clientCredential, Globals.AZURE_GRAPH_API_RESOURCE_ID); //RESOURCE_ID is "https://graph.microsoft.com/"
}
return null;
}
要接收訪問令牌並將其用於配置文件請求,您不需要來自服務器端的任何東西,您可以僅從客戶端實現 oAuth2。
使用以下 URL 登錄:
登錄成功后,用戶將重定向到帶有 access_token 參數的頁面。 然后使用以下 AJAX 調用來獲取用戶信息:
var token = login_window.location.href.split('access_token=').pop().split('&')[0]; $.ajax({ url: "https://graph.microsoft.com/v1.0/me", type: "GET", beforeSend: function(xhr){xhr.setRequestHeader('Authorization', 'Bearer '+token);}, success: function(data) { alert('Hi '+data.displayName); console.log(data); } });
請注意,您可能需要從 Azure Active Directory 應用程序清單文件啟用oauth2AllowImplicitFlow:true設置。
將 "oauth2AllowImplicitFlow": false 設置為 "oauth2AllowImplicitFlow": true。
最后,確保您的應用程序具有Microsoft Graph所需的權限,即登錄用戶和查看用戶的基本配置文件
獲取新應用程序訪問權限的更新答案:
授權響應將如下所示:
https://localhost/myapp/?code=M0ab92efe-b6fd-df08-87dc-2c6500a7f84d&state=12345
獲取令牌
POST /{tenant}/oauth2/v2.0/token HTTP/1.1
主機: https : //login.microsoftonline.com
內容類型:應用程序/x-www-form-urlencoded
client_id=6731de76-14a6-49ae-97bc-6eba6914391e
&scope=user.read%20mail.read
&code=OAAAABAAAAiL9Kn2Z27UubvWFPbm0gLWQJVzCTE9UkP3pSx1aXxUjq3n8b2JRLk4OxVXr...
&redirect_uri=http%3A%2F%2Flocalhost%2Fmyapp%2F
&grant_type=授權代碼
&client_secret=JqQX2PNo9bpM0uEihUPzyrh // 注意:僅 Web 應用程序需要
使用訪問令牌調用 Microsoft Graph
獲取https://graph.microsoft.com/v1.0/me
授權:承載 eyJ0eXAiO ... 0X2tnSQLEAnnSPHY0gKcgw
主持人:graph.microsoft.com
來源:
https://docs.microsoft.com/en-us/graph/auth-v2-user?context=graph/api/1.0
您還可以在沒有用戶的情況下獲取訪問令牌,請參見此處:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.