gpt4 book ai didi

azure - 无法从基于 Java 的 Rest API 访问 SharePoint 图形 API

转载 作者:行者123 更新时间:2023-12-03 03:59:01 25 4
gpt4 key购买 nike

我们需要通过 API 层(此处没有 Web 层)访问 Sharepoint(图形)API。我正在尝试通过基于 Java 的 Rest API 和 AAD 来访问 Graph API。我已在 Azure 中注册了我的应用程序,并且拥有 client_id 和 Secrete。

  1. grant_type=authorization_code 流程

我尝试过authorization_code流程https://learn.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow

从“/oauth2/v2.0/authorize”获取代码,然后使用该代码获取access_token在网络应用程序中运行良好,我们可以在“/oauth2/v2.0/authorize”流程中登录并接受同意页面。

但是当我尝试通过 API 访问相同内容时,会出现以下错误:

http://localhost:9090/ping?error=login_required&error_description=AADSTS50058: A silent sign-in request was sent but no user is signed in. The cookies used to represent the user's session were not sent in the request to Azure AD. This can happen if the user is using Internet Explorer or Edge, and the web app sending the silent sign-in request is in different IE security zone than the Azure AD endpoint (login.microsoftonline.com).

Timestamp: 2020-08-07 05:32:10Z&error_uri=https://login.microsoftonline.com/error?code=50058
  • grant_type=client_credentials
  • 当我使用 grant_type=client_credentials、client_id 和 client_secret 生成 access_token 时,会出现以下错误:

    应用程序访问 token 请求:

    curl -X POST \
    https://login.microsoftonline.com/<tennantid>/oauth2/v2.0/token \
    -H 'Content-Type: application/x-www-form-urlencoded' \
    -d 'grant_type=client_credentials&client_id=< client_id>&client_secret=<client_secret>&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default'
    {
    "error": {
    "code": "AccessDenied",
    "message": "Either scp or roles claim need to be present in the token.",
    "innerError": {
    "date": "2020-08-07T06:23:21",
    "request-id": "***-15e3-437d-8297-****"
    }
    }
    }

    我在我的应用程序中添加了以下权限: enter image description here

    最佳答案

    正如 Allen Wu 几分钟前评论的那样,假设您希望 Java 后端访问用户的 Sharepoint 文件(看起来这就是您想要的),您基本上不能使用“client_credentials”来实现此目的。 OAuth 概念是使用代表用户生成的 token ,并在 Azure 中连接/注册的服务中转发/使用它。

    您要做的就是坚持您测试的“authorization_code”流程,并将您获取的 token (在用户可以看到的某些前端中,无论是什么)转发到您的 Java 服务/API(在 REST 客户端中)您将其附加到 HTTP 请求)。

    在 Java 服务/API 中,您提取 token 并将其转发到图形 API。 Microsoft 还提供了在使用/转发 token 之前验证 token 的方法。最佳实践是在执行其他操作之前在安全拦截器中对 Java 服务进行验证。

    关于azure - 无法从基于 Java 的 Rest API 访问 SharePoint 图形 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63296619/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com