azure-active-directory - 通过 Graph API 访问 Office 365

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

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

第一个问题是我有一个 java 程序试图列出 Office 365 订阅中的所有用户。我正在调用 https://graph.microsoft.com/v1.0/users/,但收到 403 禁止 回复。

在应用程序注册时,我在两个委托(delegate)上添加了权限,包括 User.ReadUser.ReadBasic.AllUser.ReadWrite和应用程序权限。

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

这是导致 403 的代码片段

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());
}

这是获取 token 的方法

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;
    }

最佳答案

apps.dev.microsoft.com 中的应用注册程序可与 v2.0 端点配合使用。请单击此处了解有关 v2.0 端点的更多详细信息。

您可以使用 v2.0 authentication protocols 获取 token 和 Azure Active Directory v2.0 authentication libraries 。在身份验证过程中,您需要对 User.ReadBasic.All 权限进行用户同意或管理员同意。同意后,访问 token 包含该委托(delegate)权限,并将在调用列表用户操作时起作用。

关于azure-active-directory - 通过 Graph API 访问 Office 365,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46449371/

相关文章:

asp.net - 如何在本地测试 Azure Active Directory(回复 URL)

microsoft-graph-api - 图生命周期通知未注册正确的端点

microsoft-graph-api - 如何获取 Microsoft graph 中共享日历的列表?

microsoft-graph-api - 使用 Microsoft Graph API C# 将聊天消息发送到 Microsoft Teams channel

reactjs - 使用 adalFetch 从 API 调用返回“AADSTS500011”错误消息

azure - 通过 MS Graph 将用户添加到 AAD 组

typescript - Azure AD - MSAL.js - ssoSilent() 与 acquireTokenSilent()

powershell - 我从哪里获得 Powershell Cmdlet Remove-UserPhoto

c# - 无法静默获取 token - Microsoft Graph API 获取用户的 Outlook 组

azure-active-directory - 从所有组中检索所有用户?