【问题标题】:AAD microsoft graph, client credentialsAAD 微软图,客户端凭据
【发布时间】:2021-10-01 04:44:35
【问题描述】:

我已设置 Office 365 E3 试用帐户。我在 AAD 中注册了两个应用程序。

第一个使用“授权代码流”并按预期工作(可以访问登录的用户日历)。

第二个应用程序使用“客户端凭据流”并且不起作用。

  1. 登录浏览器(边缘)

    GET /OAuthTest3 HTTP/1.1
    
    HTTP/1.1 302 Found
    Location: https://login.microsoftonline.com/<tenant>/adminconsent?client_id=<app_id>&redirect_uri=http://localhost:1234/OAuthTest3
    
    GET /OAuthTest3?admin_consent=True&tenant=<tenant> HTTP/1.1
    
    HTTP/1.1 200 OK
    
  2. 连接到https://login.microsoftonline.com/

    POST /<tenant>/oauth2/token HTTP/1.1
    Host: login.microsoftonline.com
    
    client_id=<app_id>&
    client_secret=<client_secret>&
    grant_type=client_credentials&
    redirect_uri=http://localhost:1234/OAuthTest3&
    resource=https://graph.microsoft.com/&
    scope=https://graph.microsoft.com/calendars.readwrite
    
    
    HTTP/1.1 200 OK
    {
      "token_type": "Bearer",
      "expires_in": "3600",
      "ext_expires_in": "0",
      "expires_on": "1504333342",
      "not_before": "1504329442",
      "resource": "https://graph.microsoft.com/",
      "access_token": <token>
    }    
    
  3. 连接到https://graph.microsoft.com/

    GET /v1.0/users/<user>/calendars HTTP/1.1
    Host: graph.microsoft.com
    Authorization: Bearer <token>
    
    HTTP/1.1 403 Forbidden
    {
        "error": {
        "code": "ErrorAccessDenied",
        "message": "Access is denied. Check credentials and try again.",
            "innerError": {
                "request-id": "e7228de4-2b27-4779-abef-ccab0d88970a",
                "date": "2017-09-02T05:22:27"
            }
        }
    }
    

【问题讨论】:

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


【解决方案1】:

为了在 AAD V2.0 中使用客户端凭据流,您需要首先反对您的应用程序的管理员同意。即使您不需要使用授权码授予的相同范围的同意,也是如此。

查看v2 Endpoint and Admin Consent,了解如何获得同意。

更新:

范围与客户端凭据的工作方式不同。您需要在应用的注册中定义它们,而不是使用空格分隔列表 (https://graph.microsoft.com/user.read https://graph.microsoft.com/calendars.readwrite) 动态请求范围。

这是使用https://apps.dev.microsoft.com 门户完成的。在您的应用注册中,找到“应用程序权限”部分,然后单击“添加”按钮。这将弹出一个对话框,您可以在其中选择所需的权限:

在您的应用程序中,您还需要更改 scope 参数,以便系统知道使用您注册的范围。这是通过为范围传递https://graph.microsoft.com/.default 来完成的:

POST /<tenant>/oauth2/v2.0/token HTTP/1.1
Host: login.microsoftonline.com

client_id=<app_id>&
client_secret=<client_secret>&
grant_type=client_credentials&
redirect_uri=http://localhost:1234/OAuthTest3&
resource=https://graph.microsoft.com/&
scope=https://graph.microsoft.com/.default

重要提示:每当您更改范围时,您都必须重新执行管理员同意流程,然后才能同意这些新范围。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-10
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多