【问题标题】:Office 365 Access via Graph API通过图形 API 访问 Office 365
【发布时间】:2017-09-27 13:37:50
【问题描述】:

我在通过 Microsoft Graph API 访问我的 office 365 帐户时遇到一个(或两个)问题。

第一个问题是我有一个 java 程序试图列出 office 365 订阅中的所有用户。我打电话给https://graph.microsoft.com/v1.0/users/,但得到了403 forbidden 的回复。

在应用注册时,我在委派和应用权限上添加了包括User.ReadUser.ReadBasic.AllUser.ReadWrite 在内的权限。

我也尝试过使用图形资源管理器,但是当我进入使用我的帐户时,它仍然使用内置图形用户并且不显示我的应用程序登录信息。不确定这些是否相关。

这是导致 403 的代码 sn-p

AuthenticationResult result = getAccessTokenFromUserCredentials(RESOURCE_GRAPH, ID, PASSWORD);

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

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Authorization", "Bearer "+result.getAccessToken());
if (conn.getResponseCode() != 200) {
    throw new RuntimeException("Failed : HTTP error code : "
            + conn.getResponseCode());
}

这是获取令牌的方法

private static AuthenticationResult getAccessTokenFromUserCredentials(String resource,
                                                                          String username, String password) throws Exception {
        AuthenticationContext context;
        AuthenticationResult result = null;
        ExecutorService service = null;
        try {
            service = Executors.newFixedThreadPool(1);
            context = new AuthenticationContext(AUTHORITY, false, service);
            Future<AuthenticationResult> future = context.acquireToken(
                    resource, CLIENT_ID, username, password,
                    null);
            result = future.get();
        } finally {
            service.shutdown();
        }

        if (result == null) {
            throw new ServiceUnavailableException(
                    "authentication result was null");
        }
        return result;
    }

【问题讨论】:

  • 你如何验证用户以获取你传递给/users的令牌?
  • 更新了代码示例。如果我打电话给graph.microsoft.com/v1.0/me,我会取回代表我的用户的 json 字符串。但是如果我用 /users/ 打电话,我会得到 403。我还可以访问共享点端点并获取站点数据等。
  • User.ReadBasic.All 需要管理员同意,您是否尝试过获得管理员同意?你在哪里注册应用程序,在 azure 门户中?
  • 它在 apps.dev.microsoft.com 上注册,然后它也显示为我的用户下的应用程序在 azure 门户中。虽然,当我查看 azure 时,它​​会将其列为默认访问。但是,当我列出权限时,它会告诉我我已经读取了用户同意和管理员同意。该应用程序声明其访问权限是用户同意。我在哪里可以更改?

标签: azure-active-directory microsoft-graph-api


【解决方案1】:

apps.dev.microsoft.com 中的应用注册适用于 v2.0 端点。有关 v2.0 端点的更多详细信息,请单击此处。

您可以使用v2.0 authentication protocolsAzure Active Directory v2.0 authentication libraries 获取令牌。在身份验证期间,您需要对 User.ReadBasic.All 权限进行用户同意或管理员同意。同意后,访问令牌包含该委托权限,并在调用列表用户操作时起作用。

【讨论】:

  • 不幸的是,我使用的是 v2.0 端点和库,但这似乎是应用程序的问题。注册门户。
【解决方案2】:

好的,我想我应该发布答案。首先,最令人困惑的是,apps.dev.microsoft.com 注册似乎不起作用(即使我使用的是 V2.0 端点和版本 2 库)。

但是,当我直接使用 azure 门户注册应用程序时,这解决了问题。我随后能够正确访问该服务。

这似乎很奇怪,虽然身份验证/授权服务是我的应用程序的标准,并且非常适合访问 Sharepoint / One Drive 等,但是,当想要访问用户端点时,它只有在注册时才能工作门户网站.azure.com。

非常感谢大家的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-28
    • 2019-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多