Ola*_*ybø 6 authentication azure azure-active-directory azure-ad-msal microsoft-graph-api
我发布了这个请求:
POST https://login.microsoftonline.com:443/{my-tennant-here}/oauth2/v2.0/token HTTP/1.1
Host: login.microsoftonline.com
Content-Type: application/x-www-form-urlencoded
client_id={client id here}
&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default
&client_secret={client secret here}
&grant_type=client_credentials
这将返回:
{
"token_type": "Bearer",
"expires_in": 3599,
"ext_expires_in": 0,
"access_token": "eyJ0eX......
}
我已经使用 jwt.io 解码了令牌,它绝对不是空的。它包含 14 个声明。aud, iss, tid 等...
然后我在此请求中使用访问令牌
GET https://graph.microsoft.com/v1.0/users
Athorization: Bearer eyJ0eX...
然后我得到一个 401 Unauthorized 这个机构:
{
"error": {
"code": "InvalidAuthenticationToken",
"message": "Access token is empty.",
"innerError": {
"request-id": "",
"date": "2018-08-14T15:41:44"
}
}
}
预期结果是 200 Ok,正文包含用户列表
这是否仅仅意味着我的应用程序未经授权,并且错误消息只是具有误导性(访问令牌为空)?还是我做错了什么?
更新:我注意到虽然令牌确实包含声明,但它没有范围声明,这对我来说似乎有点奇怪。我会假设它具有 User.Read.All 范围。应用程序(客户端 ID/客户端机密)应该具有此权限。我收到的令牌中的声明具有以下声明:
aud: "https://graph.microsoft.com",
iss: "https://sts.windows.net/my tennant id",
iat: timestamp
nbf: timestamp
exp: timestamp
aio: looks like some kind of signature
app_displayname: "the expected app name"
appid: "the expected appid"
appidacr: "1"
idp: "https://sts.windows.net/...."
oid: "GUID"
sub: "GUID"
tid: "my tennant id"
uti: "value"
ver: 1.0
Run Code Online (Sandbox Code Playgroud)
对我来说,我的问题是我在请求 url 和授权标头之间放置了一个换行符,使其成为正文。
错误的:
GET https://graph.microsoft.com/v1.0/users
Authorization: Bearer {{token}}
Run Code Online (Sandbox Code Playgroud)
正确的:
GET https://graph.microsoft.com/v1.0/users
Authorization: Bearer {{token}}
Run Code Online (Sandbox Code Playgroud)
这是一个愚蠢的错误,但很容易被忽视——如果你看到这篇文章,你可能犯了一个愚蠢的错误,比如 OP(打字错误)或这个。再次检查您的请求语法!
| 归档时间: |
|
| 查看次数: |
18650 次 |
| 最近记录: |