使用 Java 中的 Office 365 REST API 构建守护程序或服务应用程序

2024-04-05

我正在尝试构建批处理作业来访问 Office 365 Mail API。正在检查文档 https://azure.microsoft.com/en-us/documentation/articles/active-directory-authentication-scenarios/#daemon-or-server-application-to-web-api(这个概念很清楚),我找不到为Java编写的代码示例。我发现了this https://github.com/AzureAD/azure-activedirectory-library-for-java/releases但它依赖于 java pom 文件,但如果可能的话,我希望直接使用 REST API 或 Graphi API。

有人可以指导我如何开始构建守护程序服务以在无需用户登录的情况下访问 Office 365 REST API 吗?

UPDATE

我有以下代码来使用 AADL 库获取令牌

String tenant="....";
        String authority = "https://login.windows.net/"+tenant+"/oauth2/authorize";
        ExecutorService service=null;
        service= Executors.newFixedThreadPool(1);

        try{

            AuthenticationContext authenticationContext= new AuthenticationContext(authority,false,service);
            String certFile="/mycert2.pfx";
            InputStream pkcs12Cert= new SharedFileInputStream(certFile);

            AsymmetricKeyCredential credential=AsymmetricKeyCredential.create("....",pkcs12Cert,"pass");


            Future<AuthenticationResult> future=authenticationContext.acquireToken("https://outlook.office365.com",credential,null);

            System.out.println("Token Received"+future.get().getAccessToken());
            String token = future.get().getAccessToken();

            HttpGet httpGet = new       HttpGet("https://graph.microsoft.com/v1.0/users");

            httpGet.setHeader("Authorization", "Bearer "+token);


            GraphServices graphServices = new GraphServices();
            ResponseEntity<String> responseEntity;


            //responseEntity = graphServices.getEmails(token); //Throws the same Unauthorized exception 

            HttpClient httpClient= HttpClients.createDefault();

            HttpResponse response=httpClient.execute(httpGet);
//response contains Unauthorized access 
            HttpEntity entity=response.getEntity();

        }
        catch (MalformedURLException e){
            e.printStackTrace();

        }
        catch (Exception e){
            e.printStackTrace();
        }


    }

这里来自 http.execute 方法的未经授权的错误

HttpResponseProxy{HTTP/1.1 401 未经授权 [内容类型: 应用程序/json;字符集=utf-8,服务器:Microsoft-IIS/8.5, 请求 ID:49ca360f-ab4b-42d5-a4b0-9676e4244c21,客户端请求 ID: 49ca360f-ab4b-42d5-a4b0-9676e4244c21,x-ms-ags-诊断: {"ServerInfo":{"数据中心":"西 US","Slice":"SliceA","ScaleUnit":"003","主机":"AGSFE_IN_8","ADSiteName":"WST"}}, X-Powered-By:ASP.NET,日期:2016 年 9 月 6 日星期二 20:43:24 GMT, 内容长度:244] ResponseEntityProxy{[内容类型: 应用程序/json; charset=utf-8,内容长度:244,分块:false]}}

eyJ0eXAiOiJKV1QiLCJxcy76FRUlljRV9tb3RXVkpLSHJ3TEJiZF85cyIsImtpZCI6IlliUkFRUlljRV9tb3RXVkpLSHJ3TEJiZF85cyJ9.eyJhdWdfsas32sub2ZmaWNlMzY1LmNvbSIsImlzcyI 6Imh0dHBzOi8vc3RzLndpbmRvd3MubmV0L2YwMjYzMzUzLWFlyjItNGE4YS1iZThhLTc3Mzc3MmE2MGJlMy8iLCJpYXQiOjE0NzMxOTQ4MjIsim5iZiI6MTQ3MzE5NDgyMiwiZXhwIjoxNDc zMTk4NzIyLCJhCHBpZCI6IjhhNjc2ZjJkLWU1M2MtNDViNy05MzhhLTdiOTE1YjVkZTRINiIsImFwcGlkYWNyIjoiMasdff4577dHMud2luZG93cy5uZXQvZjAyNjMzNTMtYWViMi00YThhLWJlOGE tNzczNzcyYTYwYmUzLyIsIm9pZCI6IjQ3NDhkZDE5LTAxOTUtNDcwOC04MTNkLTQxMTdhNDhlMTdmOCIsInN1YiI6IjQ3NDhkZDE5LTAxOTUtNDcwOC04MTNkLTQxMTdhNDhlMTdmOCIsInRpZCI6ImywMjYzMzUzL WFlyjItNGE4YS1iZThhLTc3Mzc3MmE2MGJlMyIsInZlciI6IjEuMCJ9.BKt54345DIfv2WWT4pQ--Nuy-0aHkkht4332r7E4d5mP-EAEKmcQe7y0IPjkYGZTNhyNiG2tVAyb56Gcbessdsfewz_ BNoAolTVukxttXc-pFY1_Ol5Adc8T5yio43ixfs88mrVRqZEHsb7c-wjO-otBXocZs8waYXdree83g1JtcnULs7bAGp3VBUhMjuJ2u87363Yq3lfse39_Pt6tRw]

(token和这个类似,只是为了安全改成类似的东西)


Java 演练在这里(尽管对于使用身份验证代码流的 Web 应用程序):https://dev.outlook.com/restapi/tutorial/java https://dev.outlook.com/restapi/tutorial/java

还有一个使用客户端凭据流程的示例:

https://github.com/jasonjoh/java-calendar-demo https://github.com/jasonjoh/java-calendar-demo

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Java 中的 Office 365 REST API 构建守护程序或服务应用程序 的相关文章

随机推荐