簡體   English   中英

Microsoft Graph Api令牌無效或未授權?

[英]Microsoft Graph Api token invalid or not authorized?

我正在嘗試通過Microsoft的Graph API從Azure Active Directory中讀取用戶的數據。 使用Graph Explorer,我可以吸引所有用戶,但是使用獨立的應用程序,在收到令牌后,我得到一個“未經授權”的響應。 我顯然缺少一些步驟,但是對我來說,這顯然不是什么步驟。 任何見識將不勝感激

以下代碼基於MSFT示例:

// config values
// authority = "https://login.microsoftonline.com/{ TENANT ID }/oauth2/" 
// resource uri = "https:// APP NAME .azurewebsites.net";
// graph uri = https://graph.windows.net/TENANT ID/ also tried https://graph.windows.net/v1.0/


// short form
public async void GetUsers( ADConfiguration config )   
{
     _authContext = new AuthenticationContext(config.GetAuthority());
     _clientCredential = new ClientCredential(config.ClientId, config.ClientSecret);

     AuthenticationResult result = null;

     // obtain the token, this part is still successful
     result = await _authContext.AcquireTokenAsync(config.ResourceUri, _clientCredential );

    _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);


     string address = config.GetGraphiUri() + "users?api-version=1.6";

     // this response is always unauthorized
     HttpResponseMessage response = await _httpClient.GetAsync(address);

}

除了回答您的新問題。 通過您的代碼,您正在使用客戶端憑據流來獲取令牌。 在客戶端憑據流中,權限直接授予應用程序本身。

由於使用的是Azure AD Graph API,因此需要添加application permission

  1. 在Azure門戶中,選擇您的應用程序,然后單擊“ Settings
  2. 在“設置”菜單中,選擇“ Required permissions部分,選擇“ Windows Azure Active Directory (Azure廣告圖api),添加您的應用程序所需的相關應用程序權限。
  3. 在您應用的刀片中,點擊“ Grant Permissions以使用您的管理員憑據進行管理員同意。

您的配置值似乎已關閉:

授權應該是: https://login.microsoftonline.com/{TENANT ID} : https://login.microsoftonline.com/{TENANT ID}

在我看來,您正在嘗試使用Azure AD Graph API,而不是Microsoft Graph API。

在這種情況下:

資源URI應該是: https://graph.windows.net/ : https://graph.windows.net/ (MS Graph API為https://graph.microsoft.com/

暫無
暫無

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

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