我正在使用 Microsoft Graph SDK 获取我的应用程序(而不是用户)的访问 token ,以便从共享点读取数据。我一直在关注这个document ,以及发布此SO question 。链接 SO 中的代码是相同的。我能够添加应用程序权限并在 azure 门户中授予它们(通过按按钮)。问题是,返回使用的 token 不包含任何角色/scp 声明。因此,在使用 token 时,我收到“ token 中需要存在 scp 或角色声明”消息。
为了确定,我在获取访问 token 时传递的范围的唯一值是:https://graph.microsoft.com/.default
。我没有传递像 Sites.ReadWrite.All 这样的其他内容(如果我添加该范围,我会得到一个异常)。我不确定如何继续进行故障排除,我们将不胜感激。
编辑:使用如下所示的图形 SDK 添加代码:
var client = new ConfidentialClientApplication(id, uri, cred, null, new SessionTokenCache());
var authResult = await client.AcquireTokenForClientAsync(new[] {"https://graph.microsoft.com/.default"});
var token = authResult.AccessToken;
var graphServiceClient = new GraphServiceClient(new DelegateAuthenticationProvider(async request => {request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token)}));
var drives = await graphServiceClient.Sites[<sharepoint_host>].SiteWithPath(<known_path>).Drives.Request().GetAsync();
最佳答案
似乎以不同的方式进行应用程序初始化是解决方案。而不是这个:
var client = new ConfidentialClientApplication(id, uri, cred, null, new SessionTokenCache());
这样做:
var app = new ConfidentialClientApplication(ClientId, Authority, RedirectUri, credentials, null, new TokenCache());
关于c# - 访问 token 不包含通过 Microsoft Graph 的 SCP(角色)声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55830228/