使用Java通过Microsoft图形访问Office 365规划器

DaveP:

我正在尝试获取ms计划器中的任务列表。我有一些与重定向一起使用的代码,其中提供了身份验证代码,以通过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();
}

'''

这适用于现存的Buckets调用,返回计划ID定义的计划中的存储桶

我现在希望通过不需要用户访问权限的守护程序来使代码自动化,并且身份验证代码现在为:'''public String getUserAccessToken(String [] scope){

    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。

非常感谢任何帮助/指导。

提前致谢

朱东ny:

当前不支持使用客户端凭据(无用户访问权限)访问计划程序。

您可以在Microsoft论坛(Microsoft图形功能请求上对其进行投票

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用Microsoft图形API从Office 365还原已删除的邮件

路线规划器二维阵列JAVA

通过Office 365安装Microsoft Excel 2016

使用服务帐户访问Office 365数据

图形调用更改规划器任务返回 204(并且不进行更改)

无法使用 Azure 应用程序通过图形 API 访问一个驱动器

规划器不使用索引顺序使用 CTE 对记录进行排序

JAVA中的Microsoft Office 365邮件REST API

JAVA中的Microsoft Office 365邮件REST API

Java Mail委托访问不适用于Office 365

Rails 5,使用Microsoft帐户登录(Office 365)

如何使用 Microsoft Graph Api 获取 Office 365 的邮件大小?

在Visual Basic 6.0中使用Microsoft(Office)365工具

Office 365 API OutlookServicesClient筛选器消息错误

Microsoft.Graph.GraphServiceClient规划器存储桶AddAsync返回错误“仅在包含的实体上支持写请求”

使用Meteor JS访问Exchange或Office 365 API?

使用Global Admin帐户在线访问被拒绝的Office 365 / SharePoint

使用Office365 JavaScript API访问UserProperty

无法使用工作帐户访问Office 365中的日历

Office 365的Microsoft Office自动化

Microsoft Graph API-Office 365访问SharePoint嵌套文件夹项目

使用动态规划的背包

通过Microsoft图形访问共享邮箱

通过base64字符串在Office 365图形api上上传用户个人资料照片

使用Office 365 V2 DLL后,Microsoft.Office365.Discovery-CapabilityDiscoveryResult-> ServiceEndpointUri具有错误的URi

Office365 Microsoft图全文搜索

通过REST响应Office 365事件邀请

通过EWS API连接到Office 365

无法使用smtp.office365.com中的用户名在SMTP服务器上进行身份验证