簡體   English   中英

使用java通過microsoft graph訪問office 365 planner

[英]Using java to access office 365 planner via microsoft graph

我正在嘗試在 ms planner 中獲取任務列表。 我有一些與重定向一起使用的代碼,它提供了一個身份驗證代碼以通過 ms java api PublicClientApplication 對象驗證用戶。

'''

///
///   Already defined after registering an application in azure AD
///    private static String applicationId;
///    private static String tennantId;
///
public String getUserAccessToken(String[] scopes)  {
    try {

        PublicClientApplication app;
        try {
            // Build the MSAL application object with
            // app ID and authority

            String authority = "https://login.microsoftonline.com/";
            app = PublicClientApplication.builder(applicationId)
                    .authority(authority + tennantId + "/")
                    .build();
        } catch (MalformedURLException e) {
            return null;
        }

        // Create consumer to receive the DeviceCode object
        // This method gets executed during the flow and provides
        // the URL the user logs into and the device code to enter
        Consumer<DeviceCode> deviceCodeConsumer = (DeviceCode deviceCode) -> {
            span.log(ImmutableMap.of("event", "o365-initialise-authentication-device-code-check", "", ""));
            // Print the login information to the console
            System.out.println(deviceCode.message());
        };

        // Request a token, passing the requested permission scopes
        IAuthenticationResult result = app.acquireToken(
                DeviceCodeFlowParameters
                        .builder(scopeSet, deviceCodeConsumer)
                        .build()
        ).exceptionally(ex -> {
            return null;
        }).join();

        if (result != null) {
            return result.accessToken();
        }
    return null;
}

public void getPlan(string planId)
{

            // Build a Graph client
            graphClient = GraphServiceClient.builder()
                    .authenticationProvider((IAuthenticationProvider) authProvider)
                    .logger(logger)
                    .buildClient();

    PlannerBucketCollectionPage existingBuckets = graphClient.planner().plans(planId).buckets().buildRequest().get();
}

'''

這適用於返回 planId 定義的計划中的存儲桶的 existingBuckets 調用

我現在希望通過不需要用戶訪問的守護進程自動執行代碼,並且身份驗證代碼現在是:''' public String getUserAccessToken(String[] scopes) {

    try {

        // Create default logger to only log errors
        DefaultLogger logger = new DefaultLogger();
        logger.setLoggingLevel(LoggerLevel.DEBUG);

        ConfidentialClientApplication app = ConfidentialClientApplication.builder(
                applicationId,
                ClientCredentialFactory.create(key))
                .authority(authority + tennantId + "/")
                .build();

        ClientCredentialParameters clientCredentialParam = ClientCredentialParameters.builder(
                Collections.singleton(GRAPH_DEFAULT_SCOPE))
                .build();

        CompletableFuture<IAuthenticationResult> future = app.acquireToken(clientCredentialParam);

        BiConsumer<IAuthenticationResult, Throwable> processAuthResult = (res, ex) -> {
            if (ex != null) {
                System.out.println("Oops! We have an exception - " + ex.getMessage());
            }
            else
            {
                System.out.println("Returned ok - " + res);
                System.out.println("Access Token - " + res.accessToken());
                System.out.println("ID Token - " + res.idToken());
            }
        };

        future.whenCompleteAsync(processAuthResult);
        future.join();

        CompletableFuture<Set<IAccount>> accountsRequest = app.getAccounts();
        BiConsumer<Set<IAccount>, Throwable> processAccountsResult = (res, ex) -> {
            if (ex != null) {
                System.out.println("Oops! We have an exception - " + ex.getMessage());
            }

            if ( res == null )
            {
                System.out.println("No accounts");
            }
            else
            {
                log.info("Found "+ res.size() + " accounts");
            }
        };

        accountsRequest.whenCompleteAsync(processAccountsResult);

        CompletableFuture<IAuthenticationResult> future1;
        try {
            future1 = app.acquireTokenSilently
                    (SilentParameters.builder(Collections.singleton(GRAPH_DEFAULT_SCOPE),
                            null)
                            .forceRefresh(true)
                            .build());
        } catch (MalformedURLException e) {
            e.printStackTrace();
            throw new RuntimeException();
        }

        future1.join();
        IAccount account = app.getAccounts().join().iterator().next();
        app.removeAccount(account).join();

        return future.get().accessToken();
    }
    catch ( Exception ex)
    {
        log.error("Unable to get O365 token", ex);
    }
    return null;
}

'''

但是,帳戶對象為空,如果我跳過 future1 調用,則會收到 http 錯誤 401。

感謝您收到任何幫助/指導。

提前致謝

當前不支持使用客戶端憑據(無用戶訪問權限)訪問規划器。

您可以在 Microsoft 論壇( Microsoft 圖形功能請求)上對其進行投票。

暫無
暫無

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

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