簡體   English   中英

Scope 問題在 Azure 圖 Rest API Java

[英]Scope Issue in Azure Graph Rest API Java

我是新手,使用 Azure 圖 Rest API Java 使用此 repo

我的目標是列出 AAD 租戶中的所有用戶

到目前為止,我只能做到這一點:

List<String> scopes= Arrays.asList("https://graph.microsoft.com/User.Read.All");
   

    AzureProfile profile = new AzureProfile(tenantId, subscriptionId, AzureEnvironment.AZURE);
    final ClientSecretCredential credential = new ClientSecretCredentialBuilder()
            .clientId(clientId)
            .clientSecret(clientSecret)
            .tenantId(tenantId)
            //.httpClient(client)
            .authorityHost(profile.getEnvironment().getActiveDirectoryEndpoint())
            .build();

    TokenCredentialAuthProvider tokenCredentialAuthProvider = new TokenCredentialAuthProvider(scopes, credential);


    GraphServiceClient<Request> graphClient =
            GraphServiceClient
                    .builder()
                    .authenticationProvider(tokenCredentialAuthProvider)
                    .buildClient();


    UserCollectionPage users = graphClient.users()
            .buildRequest()
            .get();

   for(User user: users.getCurrentPage()){

       System.out.println(user.displayName);
       System.out.println(user.id);
       System.out.println(user.userPrincipalName);

   }

但是,我遇到了這個錯誤:

引起:java.io.IOException:java.util.concurrent.ExecutionException:com.microsoft.aad.msal4j.MsalServiceException:
AADSTS1002012:為 scope https://graph.microsoft.com/User.Read.All openid 配置文件 offline_access 提供的值無效。 客戶端憑據流必須具有 scope 值,並在資源標識符(應用程序 ID URI)后加上 /.default 后綴。

看來我用過的 Scope 是錯誤的/不夠用的,但我不太確定我應該用什么來搭配 scope。 任何的想法?

我嘗試通過 Postman 在我的環境中重現相同內容,並得到以下結果:

我注冊了一個 Azure AD 應用程序並添加了API permissions ,如下所示:

在此處輸入圖像描述

當我嘗試使用客戶端憑據流通過 Postman 生成與您相同的 scope訪問令牌時,我遇到了如下相同的錯誤

POST https://login.microsoftonline.com/<tenantID>/oauth2/v2.0/token

grant_type:client_credentials
client_id: <appID>
client_secret: <secret_value>
scope: https://graph.microsoft.com/User.Read.All openid profile offline_access

回復:

在此處輸入圖像描述

解決上述錯誤,如果您使用的是客戶端憑據流,則必須將 scope 更改為https://graph.microsoft.com/.default

更改 scope 后,我能夠像下面這樣成功生成訪問令牌

POST https://login.microsoftonline.com/<tenantID>/oauth2/v2.0/token

grant_type:client_credentials
client_id: <appID>
client_secret: <secret_value>
scope: https://graph.microsoft.com/.default

回復:

在此處輸入圖像描述

當我使用上面的令牌調用下面的圖形查詢時,我成功地獲得了具有顯示名稱、ID 和用戶主體名稱的用戶列表,如下所示:

GET https://graph.microsoft.com/v1.0/users?$select=displayName,id,userPrincipalName

回復:

在此處輸入圖像描述

在您的情況下,更改代碼中的scope值,如下所示:

List<String> scopes= Arrays.asList("https://graph.microsoft.com/.default");

文檔中寫道:

客戶端服務中的客戶端憑據請求必須包含 scope={resource}/.default。 這里,{resource} 是 web API,您的應用程序打算調用它,並希望為其獲取訪問令牌。 不支持使用單獨的應用程序權限(角色)發出客戶端憑據請求。 為該 web API 授予的所有應用程序角色(應用程序權限)都包含在返回的訪問令牌中。

Client Credential 流程最適合您擁有 Deamon 應用程序的情況,該應用程序必須通過非交互方式進行身份驗證並訪問某種資源,這依次意味着該 Deamon 應用程序的權限已配置並同意在授權請求之前完成的步驟。

/.default scope 可以翻譯為無人值守運行的后台應用程序的請求,以獲取為其配置的大部分權限並訪問其請求的資源。

簡而言之,在客戶端憑證流程中使用上述 scope 是一種約定,在選擇此流程時必須始終實施:P。

暫無
暫無

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

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