簡體   English   中英

嘗試對Microsoft Azure Graph進行OAUTH身份驗證WebClient時收到403錯誤

[英]403 error received when trying to OAUTH authenticate WebClient against Microsoft Azure Graph

我正在嘗試編寫一個簡單的控制台應用程序,它將在不需要用戶名/密碼的情況下使用針對Azure Graph的OAUTH進行身份驗證,但是我在執行WebClient.DownloadString方法時收到403錯誤。 任何幫助將不勝感激。

 static void Main(string[] args)
 {
  //  Constants
  var tenant = "mytenant.onmicrosoft.com";
  var resource = "https://graph.microsoft.com/";
  var clientID = "blah-blah-blah-blah-blah";
  var secret = "blahblahblahblahblahblah";

  //  Ceremony
  var authority = $"https://login.microsoftonline.com/{tenant}";
  var authContext = new AuthenticationContext(authority);
  var credentials = new ClientCredential(clientID, secret);


  // Obtain Token
  var authResult = authContext.AcquireToken(resource, credentials);

  WebClient webClient1 = new WebClient();
  webClient1.Headers[HttpRequestHeader.Authorization] = "Bearer " + authResult.AccessToken;

 webClient1.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
 webClient1.Headers[HttpRequestHeader.Accept] = "application/json";

 string payload = webClient1.DownloadString("https://graph.microsoft.com/v1.0/users?$Select=givenName,surname");

  }
 }

現在已經解決了。 上面的代碼是正確的,但是我缺少一個步驟,那就是在Azure中配置ServicePrincipal:

  1. 使用命令Connect-Msolservice以全局管理員身份登錄
  2. 檢索服務主體的對象ID> Get-MsolServicePrincipal –AppPrincipalId YOUR_APP_CLIENT_ID
  3. 使用> Add-MsolRoleMember -RoleMemberType ServicePrincipal -RoleName'Company Administrator'-RoleMemberObjectId YOUR_OBJECT_ID分配角色

以下鏈接也非常有用:

https://developer.microsoft.com/zh-cn/graph/docs/concepts/overview (單擊左上方的箭頭以顯示完整列表,然后向下滾動至相應的操作)

https://msdn.microsoft.com/zh-CN/library/azure/ad/graph/howto/azure-ad-graph-api-error-codes-and-error-handling

暫無
暫無

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

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