java - 尝试使用获取的访问 token 调用 Office 365 API 时出现 401 Unauthorized

标签 java azure office365 azure-active-directory microsoft-graph-api

我正在使用 Java 创建一个简单的守护程序或服务应用程序(不是 Web 应用程序)来调用 Office 365 日历 API。我已遵循本指南 Call Microsoft Graph in a service or daemon app ,但是当我尝试使用访问 token 调用 API 时,出现 401 错误。 我已使用所有 Graph 授权将应用程序注册到 azure 门户,并按照本指南的步骤 1 制作了证书:Get a certificate or create a self-signed certificate 。这是我的访问 token 请求代码:`

    String accessToken="";
    String token_endpoint = "https://login.windows.net/<mytenant>/oauth2/token";
    String grant_type = "client_credentials";
    String client_secret = <mysecret>;
    String resource = "https://graph.microsoft.com";
    String client_id = <myclient>;

    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(token_endpoint);
    List<NameValuePair> parameters = new ArrayList<>();
    parameters.add(new BasicNameValuePair("grant_type", grant_type));
    parameters.add(new BasicNameValuePair("client_id", client_id));
    parameters.add(new BasicNameValuePair("client_secret", client_secret));
    parameters.add(new BasicNameValuePair("resource", resource));
    httpPost.setEntity(new UrlEncodedFormEntity(parameters));
    httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");

    try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
        System.out.println(response.getStatusLine());
        HttpEntity entity = response.getEntity();

        //parsing
        JSONParser parser = new JSONParser();
        Scanner httpResponseScanner = new Scanner(entity.getContent());
        String jsonString = httpResponseScanner.nextLine();
        //System.out.println(jsonString);
        JSONObject json = (JSONObject) parser.parse(jsonString);
        accessToken = json.get("access_token").toString();

        EntityUtils.consume(entity);
    }
    return accessToken;`

这是我的 API 调用代码:`

     String apiURL = "https://outlook.office.com/api/v2.0/me/calendars";
     CloseableHttpClient httpclient = HttpClients.createDefault();
     HttpGet httpGet = new HttpGet(apiURL);
     httpGet.addHeader("Accept", "application/json");
     httpGet.addHeader("Authorization", "Bearer " + accessToken);

     try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
         System.out.println(response.getStatusLine());
         HttpEntity entity = response.getEntity();
         EntityUtils.consume(entity);
     }`

我已经使用无效的签名响应测试了 jwt.io 的访问 token ,因此我认为我的 token 请求存在问题。有人可以帮助我吗?

最佳答案

从您的代码中,您正在获取资源的 token :https://graph.microsoft.com ,但在使用该 token 的 api 调用中,您正在调用 Outlook 邮件剩余 api (https://outlook.office.com/)。如果您想调用 microsoft graph api ( https://graph.microsoft.com ),您应该检查 microsoft graph api get calendars

第二个问题是您正在使用客户端凭据流(应用程序的身份),您不能使用用户身份( /me ),因为访问 token 中不包含用户信息。使用 microsoft graph api ,您可以使用 GET /users/{id | userPrincipalName}/calendars

关于java - 尝试使用获取的访问 token 调用 Office 365 API 时出现 401 Unauthorized,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44699283/

相关文章:

sql - 在 Java 中的 SQL Azure 报告上进行表单例份验证登录

outlook - 如何在 Laravel 中创建 Outlook 日历事件?

azure - 为什么使用 Office 365 应用程序注册工具创建的应用程序在 Azure 管理门户中不可见?

java - Json 和抽象类 'Can not construct instance'

Java压缩集合库

java - 由 : java. lang.ClassNotFoundException : org. apache.commons.io.FileUtils、maven pom.xml 引起

java - 仅在 jUnit 的情况下获取 java.lang.NoSuchMethodException : io. jsonwebtoken.impl.crypto.MacProvider.generateKey

azure - 在 ubuntu 16.04 中安装 Varnish

visual-studio - Azure Web 应用程序网关性能与负载测试

oauth-2.0 - Office 365 - 使用非交互式服务应用程序处理 IMAP 基本身份验证的停用