Azure active directory AAD microsoft图形、客户端凭据

Azure active directory AAD microsoft图形、客户端凭据,azure-active-directory,microsoft-graph-api,Azure Active Directory,Microsoft Graph Api,我已经设置了一个Office 365 E3试用帐户。我在AAD注册了两个应用程序 第一个使用“授权代码流”,并按预期工作(可以访问登录用户日历) 第二个应用程序使用“客户端凭据流”,无法工作 1. login in Browser (Edge) GET /OAuthTest3 HTTP/1.1 HTTP/1.1 302 Found Location: https://login.microsoftonline.com/<tenant>/adminconsent?client_id

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

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

第二个应用程序使用“客户端凭据流”,无法工作

1. login in Browser (Edge)

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. connect to 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. connect to 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"
    }
  }
}
1。在浏览器中登录(Edge)
GET/OAuthTest3HTTP/1.1
找到HTTP/1.1 302
地点:https://login.microsoftonline.com//adminconsent?client_id=&redirect_uri=http://localhost:1234/OAuthTest3
GET/OAuthTest3?管理员同意=True&tenant=HTTP/1.1
HTTP/1.1200ok
2.连接到https://login.microsoftonline.com/
POST//oauth2/token HTTP/1.1
主机:login.microsoftonline.com
客户识别码=&
客户机密=&
授予\类型=客户端\凭据&
重定向\u uri=http://localhost:1234/OAuthTest3&
资源=https://graph.microsoft.com/&
范围=https://graph.microsoft.com/calendars.readwrite
HTTP/1.1200ok
{
“令牌类型”:“承载者”,
“expires_in”:“3600”,
“ext_expires_in”:“0”,
“到期日”:“1504333342”,
“不在之前”:“1504329442”,
“资源”:https://graph.microsoft.com/",
“访问令牌”:
}
3.连接到https://graph.microsoft.com/
GET/v1.0/users//calendars HTTP/1.1
主持人:graph.microsoft.com
授权:持票人
HTTP/1.1 403禁止
{
“错误”:{
“代码”:“ErrorAccessDenied”,
“消息”:“访问被拒绝。请检查凭据并重试。”,
“内部错误”:{
“请求id”:“e7228de4-2b27-4779-abef-ccab0d88970a”,
“日期”:“2017-09-02T05:22:27”
}
}
}
谢谢


Emil

为了在AAD V2.0中使用客户端凭据流,您需要首先获得应用程序的对象管理员同意。即使使用授权代码grant不需要相同范围的同意,这也是正确的

查看获取同意的详细信息

更新:

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

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

在应用程序中,您还需要更改
范围
参数,以便系统知道如何使用您注册的范围。这是通过传递
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
POST//oauth2/v2.0/token HTTP/1.1
主机:login.microsoftonline.com
客户识别码=&
客户机密=&
授予\类型=客户端\凭据&
重定向\u uri=http://localhost:1234/OAuthTest3&
资源=https://graph.microsoft.com/&
范围=https://graph.microsoft.com/.default
重要提示:任何时候更改作用域时,都必须重新执行管理员同意流,然后才能同意这些新作用域


你好,埃米尔。我想您正在使用V2端点,也使用哪个库?嗨,Jean Marc,我使用端点进行授权。以及graph.microsoft.com/v1.0/users/,用于graph api。我正在实现我自己的库。我用协议实现的更多细节更新了我的问题。太棒了,这非常有用。我已经更新了我的答案。非常感谢你的帮助。我现在可以访问自己的日历和其他用户的日历。