问题描述
我有一个 Web 应用程序,它使用仅应用程序令牌来覆盖最终用户检索租户中所有网站集的权限。尝试使用 the example 中提供的样板代码并稍作更改时,Graph API 在尝试发出调用 accessDenied
时返回 https://graph.microsoft.com/v1.0/sites?search=*
。如果我删除 WithAppOnly()
,则调用成功[如果已分配 Sites.Read.All 的委派权限]。已向 Azure AD 注册应用分配了管理员批准的应用范围 Sites.Read.All
。
var queryOptions = new List<QueryOption>()
{
new QueryOption("search","*")
};
var sites = await graphServiceClient.Sites.Request(queryOptions)
.WithAppOnly()
.WithScopes("Sites.Read.All")
.GetAsync();
ServiceException: Code: accessDenied
Message: Access denied
Inner error:
AdditionalData:
date: 2021-03-20T21:45:27
request-id: 16933bd6-5e7f-4820-9563-fec75575c9b2
client-request-id: 16933bd6-5e7f-4820-9563-fec75575c9b2
ClientRequestId: 16933bd6-5e7f-4820-9563-fec75575c9b2
解决方法
您需要在 Azure 门户中添加 应用程序 的 Sites.Read.All 权限。
尝试使用 client credentials flow 进行测试。
// Get access token
POST https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token
Content-Type: application/x-www-form-urlencoded
client_id={client_id}
&scope=https://graph.microsoft.com/.default
&client_secret={client_secret}
&grant_type=client_credentials
// Call MS Graph API
GET https://graph.microsoft.com/v1.0/sites?search={query}