簡體   English   中英

從 Angular 應用程序檢索訪問令牌后,嘗試在 Java 中調用 Microsoft Graph API

[英]Trying to call Microsoft Graph API in Java after retrieving Access token from Angular app

我嘗試在網上學習一些示例,但它們並不是很有幫助,而且官方的 Microsoft 文檔可能有些混亂,而且到處都是。

我有令牌,其中包括 header 中的隨機數。

我已經在我的 pom.xml 文件中添加了 Microsoft Graph 依賴項,並且一切都已設置,但我不確定如何使用訪問令牌實際實現該方法並進行調用以獲取登錄的用戶信息。

https://graph.microsoft.com/v1.0/me

我已經在 Azure 中設置了我的應用程序,並添加了 API 權限。

任何朝着正確方向的幫助或指導都會有所幫助。

您可以參考登錄用戶請求的示例,請參見此處

IGraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider( authProvider ).buildClient();

// for "/me" endpoint    
User user = graphClient.me().buildRequest().get(); 

// for "/users/{id | userPrincipalName}" endpoint
// User user = graphClient.users("{id}").buildRequest().get() 

用accessToken獲取authProvider,參考這篇文章

public IGraphServiceClient getAuthProvider() {
    IAuthenticationProvider mAuthenticationProvider;

    try {
        String accessToken = "xxxxxxxxxxxxx";
        mAuthenticationProvider = request -> request.addHeader("Authorization", "Bearer " + accessToken);
    } catch (Exception e) {
        throw new Error("Could not create a graph client: " + e.getLocalizedMessage());
    }

    return GraphServiceClient.builder()
                             .authenticationProvider(mAuthenticationProvider)
                             .buildClient();
}

有關詳細信息, 請使用帶有 Java 的 Microsoft Graph SDK進行 API 調用。

暫無
暫無

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

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