【发布时间】:2021-10-05 19:01:30
【问题描述】:
我在订阅客户群中的 callRecords 时遇到问题,我的响应状态为 Forbidden(请参阅帖子末尾)。
我做了这些步骤:
- 向 CallRecords.Read.All 注册一个应用程序注册并获得管理员同意
- 在尝试发送 POST 请求时,它在我的任何编码程序中都不起作用,但在 Postman 中它使用应用程序权限。
它适用于 Postman,但不适用于 Azure Functions(本地启动)或其他编码应用程序,需要获取合适的不记名令牌。如果使用我从程序例程获得的令牌发送 Post-request,我会收到 Forbidden as Response 消息。
HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, new Uri("https://login.microsoftonline.com/" + TenantId + "/oauth2/v2.0/token"));
List<KeyValuePair<string, string>> parameters = new List<KeyValuePair<string, string>>();
parameters.Add(new KeyValuePair<string, string>("client_id", ClientId));
parameters.Add(new KeyValuePair<string, string>("scope", "https://graph.microsoft.com/.default"));
parameters.Add(new KeyValuePair<string, string>("client_secret", ClientSecret));
parameters.Add(new KeyValuePair<string, string>("grant_type", "client_credentials"));
request.Content = new FormUrlEncodedContent(parameters);
HttpResponseMessage response = await client.SendAsync(request);
string data = await response.Content.ReadAsStringAsync();
Token = JsonConvert.DeserializeObject<TokenResponse>(data);
清单 1:获取访问令牌
我分析了我用 jwt.ms 获得的这个令牌(ID 和其他信息用 *** 标记)
"typ": "JWT",
"nonce": "***",
"alg": "RS256",
"x5t": "l3sQ-50cCH4xBVZLHTGwnSR7680",
"kid": "l3sQ-50cCH4xBVZLHTGwnSR7680"
}.{
"aud": "https://graph.microsoft.com",
"iss": "https://sts.windows.net/***/",
"iat": 1633425547,
"nbf": 1633425547,
"exp": 1633429447,
"aio": "***",
"app_displayname": "***",
"appid": "***",
"appidacr": "1",
"idp": "https://sts.windows.net/***/",
"idtyp": "app",
"oid": "***",
"rh": "***",
"sub": "***",
"tenant_region_scope": "EU",
"tid": "***",
"uti": "***",
"ver": "1.0",
"wids": [
"0997a1d0-0d1d-4acb-b408-d5ca73121e90"
],
"xms_tcdt": 1373376639
}.[Signature]
令牌的 JSON 信息
代码中的令牌与我从邮递员应用程序获得的令牌之间的区别是 SCP : "CallRecords.Read.All"
然后我发现,如果我使用具有委托权限的应用程序注册 User.read.All 对我有用,如果我有一个有效的用户登录了相关的租户,那么创建呼叫记录订阅成功。 但是在客户端,我们只有一个应用程序注册+密钥,具有 callrecords.read.all 和 User.read.all 权限。在客户案例中,我每次都在未经许可的情况下获得令牌。并且在租户中无法重定向到邮递员回调 url。
我阅读了文档 https://docs.microsoft.com/de-de/graph/sdks/choose-authentication-providers?tabs=CS#client-credentials-provider 和相应的链接,但我没有了解我必须做什么。
我尝试了 youtube 视频 https://www.youtube.com/watch?v=Z1xFjmttEvY 用于发送此帖子的逻辑应用程序 - 步骤与创建客户应用程序注册相似。但它也失败了(同样的错误)。 我用 https://graph.microsoft.com/v1.0/subscriptions 与身体:
{
"resource": "/communications/callRecords",
"changeType": "created",
"clientState": "clientStateValue",
"notificationUrl": " working URLendpoint>",
"expirationDateTime": "2021-09-28T18:58:05.9125505Z",
"latestSupportedTlsVersion": "v1_2"
}
{
"error": {
"code": "ExtensionError",
"message": "Operation: Create; Exception: [Status Code: Forbidden; Reason: The request is not authorized for this user or application.]",
"innerError": {
"date": "2021-10-05T21:47:03",
"request-id": "aa624900-02bb-4b06-92ba-755889b1f459",
"client-request-id": "aa624900-02bb-4b06-92ba-755889b1f459"
}
}
}
BadRequest. Http request failed as there is an error getting AD OAuth token: 'AADSTS7000112: Application '***'(***-***-***-***-***) is disabled. Trace ID: ***-***-**-***-**Correlation ID: ***-***-***-***-***Timestamp: 2021-10-05 22:58:29Z'.
通过 更新这件事 但它不会伤害邮递员,它可以。为什么会这样?为什么我可以复制这种行为?
请有人告诉我我做错了什么或我必须做什么,以便我可以像邮递员那样获取令牌,就像作为应用程序请求一样?
【问题讨论】:
标签: microsoft-graph-api access-token change-notification