【问题标题】:Microsoft Graph API content 401 UnauthorizedMicrosoft Graph API 内容 401 未经授权
【发布时间】:2017-05-05 22:20:33
【问题描述】:

我正在使用客户端 ID 和密码进行身份验证。

String url = String.format("https://login.microsoftonline.com/%s/oauth2/token", tenantContext);
AuthenticationContext context = new AuthenticationContext(url, true, ForkJoinPool.commonPool());
AuthenticationResult result = context.acquireToken("https://graph.microsoft.com", new ClientCredential(clientId, clientSecret), null).get();     
String token = result.getAccessTokenType() + " " + result.getAccessToken();

我的应用程序已勾选所有权限框,并且使用上述令牌,我可以列出用户并遍历他们的驱动器和文件夹。我可以访问内容 https://graph.microsoft.com/v1.0/drives/%s/items/%s/content 在 Location 标头中返回另一个 URL。但是,当我尝试获取该 URL 时,它会返回 401 Unauthorized。

【问题讨论】:

  • 或者,项目 json 包含一个格式为:"@microsoft.graph.downloadUrl": "https://{tenant}-my.sharepoint.com/personal/{user}_{tenant}_onmicrosoft_com/_layouts/15/download.aspx?UniqueId=...&access_token=...&prooftoken=..." 的字段,看起来您应该能够访问,但这也不起作用。

标签: microsoft-graph-api


【解决方案1】:

其他 URL 是否也在 graph.microsoft.com 上?如果没有,那么您需要为该 URL 获取一个新的身份验证令牌,然后在您的下载请求中使用它。

就像您已经在使用 graph.microsoft.com 一样,但使用另一个服务器名称:

AuthenticationResult result = context.acquireToken("https://onedrive-server-name", new ClientCredential(clientId, clientSecret), null).get();     

【讨论】:

  • 我尝试了位置 url 资源的令牌 - https://{tenant}-my.sharepoint.com 但仍然得到 401。在 https://manage.windowsazure.com 我已将 Graph、O365 Sharepoint Online 和 Azure AD 添加到我的应用程序中,并具有所有读取权限。
【解决方案2】:

确保您已添加以下权限: Files.ReadAll 或 Files.ReadWriteAll 撤消应用访问权限并重试 (https://portal.office.com/account/#apps)

但是“@microsoft.graph.downloadUrl”应该可以直接使用,也许您在库设置中禁用了离线访问?

【讨论】:

  • 我无法使用ClientCredentialAsymmetricKeyCredential 甚至使用完整登录的刷新令牌下载。我得到了@microsoft.graph.downloadUrl,然后得到了 401。认为下载可能在哪里被禁用?
【解决方案3】:

我通过登录管理员用户并获得从app authentication using Azure AD获取的刷新令牌成功地从用户那里下载内容

然后可以使用 adal4j 兑换刷新令牌:

AuthenticationContext context = new AuthenticationContext(url, true, ForkJoinPool.commonPool());
AuthenticationResult result = context.acquireTokenByRefreshToken(refreshToken, new ClientCredential(clientId, clientSecret), "https://graph.microsoft.com", null).get();
String token = result.getAccessTokenType() + " " + result.getAccessToken();

使用此访问令牌,您可以通过以下方式获取文件内容: https://graph.microsoft.com/v1.0/drives/{driveid}/items/{itemid}/content

还有一个重要的额外步骤 - 管理员用户必须添加为您正在访问其内容的用户的网站集所有者。这在this blog 中有描述。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-23
    • 1970-01-01
    • 2019-03-07
    • 2017-09-27
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多