【问题标题】:Azure Graph API 403 Error using Java tutorial使用 Java 教程的 Azure Graph API 403 错误
【发布时间】:2016-12-22 16:24:22
【问题描述】:

我一直在研究文档:

https://docs.microsoft.com/en-us/azure/active-directory/active-directory-devquickstarts-webapp-java

但是当我运行项目时,我被重定向到 ADFS 登录页面,并且在身份验证后我收到此错误:

java.io.IOException:服务器返回 HTTP 响应代码:403 用于 URL:https://graph.windows.net/swisherint.onmicrosoft.com/users?api-version=2013-04-05

当我从本地主机运行时出现此错误。我还将示例应用程序部署到 Azure 并得到相同的错误。

我已在活动目录 > 应用注册 > 所需权限中添加了具有读取目录权限的 Graph API 权限。我还添加了 Windows Azure Active Directory 权限(登录并读取用户配置文件)

这是一个常见错误吗?我是否使用了错误版本的 Graph API?我已经尝试了其他问题的几种解决方案,但没有奏效。

【问题讨论】:

    标签: java azure graph


    【解决方案1】:

    Azure Graph API 似乎需要 URI 连接类型,而不是 java 教程使用的 HttpUrlConnection。这在没有 403 错误的情况下有效:

           try{
            // OAuth2 is required to access this API. For more information visit:
            // https://msdn.microsoft.com/en-us/office/office365/howto/common-app-authentication-tasks
    
            // Specify values for path parameters (shown as {...})
            URIBuilder builder = new URIBuilder("https://graph.windows.net/swisherint.onmicrosoft.com/users");
            // Specify values for the following required parameters
            builder.setParameter("api-version", "1.6");
            // Specify values for optional parameters, as needed
            // builder.setParameter("$filter", "startswith(displayName,'A')");
            URI uri = builder.build();
            HttpGet request = new HttpGet(uri);
            HttpResponse response = httpclient.execute(request);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                System.out.println(EntityUtils.toString(entity));
            }
    
            users =  EntityUtils.toString(entity);
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
        }
    

    感谢您的回复!

    知识库

    【讨论】:

    • 完美运行!谢谢
    【解决方案2】:

    根据 AAD Graph API Get Users 的新官方文档参考,代码中的api-version 属性似乎应该更改为1.6。请尝试一下。

    同时,还有一个Error code reference 列表,您可以在其中找到AAD Graph API 调用的常见错误代码403 的描述。并检查您的问题是否属于Authentication_UnauthorizedAuthorization_RequestDeniedDirectory_QuotaExceeded 错误之一。

    如有任何更新,请随时告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-15
      • 1970-01-01
      • 1970-01-01
      • 2018-08-15
      • 2020-07-31
      • 1970-01-01
      • 2020-05-03
      • 1970-01-01
      相关资源
      最近更新 更多