我正在使用仅限应用流成功检索Microsoft图形 API 的访问令牌,但生成的令牌似乎无法访问任何内容。
这是我正在使用的身份验证代码:
var clientApp = new ConfidentialClientApplication(
identifier,
authority,
"urn:ietf:wg:oauth:2.0:oob",
new ClientCredential(secret), null, null);
var scopes = new string[] { $"{identifier}/.default" };
AuthenticationResult authResult = await clientApp.AcquireTokenForClientAsync(scopes);
return authResult.AccessToken;
从中,我确实得到了一个令牌,但是当我尝试使用它时,它会抛出Access token validation failure.
这是我一直在使用的测试查询:
var users = service.Users.Request()
.Filter($"mail eq '{resourceIdentifier}'")
.Top(1)
.GetAsync();
users.Wait();
对于API baseUrl
,我提供了:https://graph.windows.net/{appId}
。我确实向查询字符串添加了api-version=1.6
(手动,因为我没有看到通过 Microsoft.Graph NuGet 库公开的选项(。我之前尝试过https://graph.microsoft.com/v2.0
,也无济于事。
无论如何,鉴于有关validation failure,
的错误消息,我开始相信我们的(可能是特定于租户的?API URI 可能是错误的。难道是这样?我没有看到什么?
更新
该解决方案有两个组件。第一个是公认的答案中提到的。第二个是范围应该简单地https://graph.microsoft.com/.default
,尽管我的 API 调用是特定于租户的。
您在这里将两个不同的 API 混为一谈。
graph.windows.net
URI 适用于 Azure AD Graph,这是一个完全不同的 API。对于Microsoft图,URI 应graph.microsoft.com
。
今天也没有Microsoft图的/v2.0
。公开可用的版本是 /v1.0
和 /beta
.另请注意,使用适用于 .NET 的 Microsoft 图形客户端库时,无需提供baseUrl
,因为它已默认为 https://graph.microsoft.com/v1.0
。