簡體   English   中英

AAD 微軟圖,客戶端憑據

[英]AAD microsoft graph, client credentials

我已經設置了一個 Office 365 E3 試用帳戶。 我在 AAD 中注冊了兩個應用程序。

第一個使用“授權代碼流”並按預期工作(可以訪問登錄用戶日歷)。

第二個應用程序使用“客戶端憑據流”並且不起作用。

  1. 登錄瀏覽器(Edge)

     GET /OAuthTest3 HTTP/1.1 HTTP/1.1 302 Found Location: https://login.microsoftonline.com/<tenant>/adminconsent?client_id=<app_id>&redirect_uri=http://localhost:1234/OAuthTest3 GET /OAuthTest3?admin_consent=True&tenant=<tenant> HTTP/1.1 HTTP/1.1 200 OK
  2. 連接到https://login.microsoftonline.com/

     POST /<tenant>/oauth2/token HTTP/1.1 Host: login.microsoftonline.com client_id=<app_id>& client_secret=<client_secret>& grant_type=client_credentials& redirect_uri=http://localhost:1234/OAuthTest3& resource=https://graph.microsoft.com/& scope=https://graph.microsoft.com/calendars.readwrite HTTP/1.1 200 OK { "token_type": "Bearer", "expires_in": "3600", "ext_expires_in": "0", "expires_on": "1504333342", "not_before": "1504329442", "resource": "https://graph.microsoft.com/", "access_token": <token> }
  3. 連接到https://graph.microsoft.com/

     GET /v1.0/users/<user>/calendars HTTP/1.1 Host: graph.microsoft.com Authorization: Bearer <token> HTTP/1.1 403 Forbidden { "error": { "code": "ErrorAccessDenied", "message": "Access is denied. Check credentials and try again.", "innerError": { "request-id": "e7228de4-2b27-4779-abef-ccab0d88970a", "date": "2017-09-02T05:22:27" } } }

為了在 AAD V2.0 中使用客戶端憑據流,您需要先獲得您的應用程序的 object 管理員許可。 即使您不需要同意使用授權碼授予相同的 scope,也是如此。

查看v2 Endpoint and Admin Consent ,了解有關獲得同意的演練。

更新:

范圍與客戶端憑證的工作方式不同。 您無需使用空格分隔列表 ( https://graph.microsoft.com/user.read https://graph.microsoft.com/calendars.readwrite ) 動態請求范圍,而是需要在您的應用注冊中定義它們。

這是使用https://apps.dev.microsoft.com門戶網站完成的。 在您的應用程序注冊中,找到“應用程序權限”部分並單擊“添加”按鈕。 這將彈出一個對話框,您可以在其中 select 您需要的權限:

權限

在您的應用程序中,您還需要更改scope參數,以便系統知道使用您注冊的范圍。 這是通過為 scope 傳遞https://graph.microsoft.com/.default來完成的:

POST /<tenant>/oauth2/v2.0/token HTTP/1.1
Host: login.microsoftonline.com

client_id=<app_id>&
client_secret=<client_secret>&
grant_type=client_credentials&
redirect_uri=http://localhost:1234/OAuthTest3&
resource=https://graph.microsoft.com/&
scope=https://graph.microsoft.com/.default

重要提示:任何時候您對范圍進行更改,您都必須重新執行管理員同意流程,然后才能同意這些新范圍。

暫無
暫無

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

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