簡體   English   中英

使用委托權限調用 MS Graph 不起作用,而應用程序權限可以

[英]Calling MS Graph with delegated privileges doesn't work while application privileges do

我嘗試使用圖形 api 調用獲取用戶的自定義屬性:

我已經使用User.ReadWrite.All Delegated 權限設置了一個應用程序注冊,如文檔授予的管理員權限中所述:

在此處輸入圖像描述

使用 python,我可以使用我的應用程序注冊密碼獲取令牌:

def retrieve_token(endpoint, client_id, client_secret):
    # endpoint = "https://login.microsoftonline.com/<tenant-id>/oauth2/token"
    payload = {
        "grant_type":"client_credentials",
        "client_id": client_id,
        "client_secret": client_secret,
        "resource":"https://graph.microsoft.com"
    }
    r = requests.request("POST", endpoint,data=payload).json()["access_token"]
    return r # returns a valid jwt token

然后我使用該令牌調用 function 來獲取用戶的屬性:

def get_user_role(endpoint,user_uid, token):
    # endpoint = "https://graph.microsoft.com/v1.0/users/"
    url = endpoint + user_uid 
    headers = {
        "Authorization": "Bearer " + token, 
        "Content-Type": "application/json"
    }
    return requests.request("GET", url, headers=headers).json()

由於某種原因,它缺乏特權:

{'error': {'code': 'Authorization_RequestDenied', 'message': 'Insufficient privileges to complete the operation.', 'innerError': {'date': '2022-03-29T16:11:38', 'request-id': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', 'client-request-id': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx'}}}

如果我 go 回到我的 api 的權限並將User.ReadWrite.All委派權限更改為應用程序權限,它就可以工作。

根據我在這種情況下閱讀的內容,我應該使用委托權限,但如何讓它發揮作用?

如果您使用的是client credentials follow (使用客戶端 ID 和客戶端密碼進行身份驗證),因此您應該需要Application permissions來獲取令牌並調用 function 來獲取我的用戶屬性。 因此,不可能使用委托權限調用帶有客戶端 Crendetial 令牌的 API。

  • 正如junnas在評論中所述是正確的當您想以登錄用戶身份調用 Web API 時,您通常會使用委派權限。

  • 應用程序權限:您的應用程序需要直接訪問 web API(無用戶上下文)。

因此,您應該更改獲取訪問令牌的方式,例如使用授權代碼流或設備代碼流以登錄用戶身份獲取訪問令牌

您可以參考此文檔可以幫助調用 web API 的桌面應用程序:使用用戶名和密碼獲取令牌

暫無
暫無

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

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