我在Azure中创建了一个应用程序,并将其设置为使用Access和ID令牌。
我想连接到其他租户并阅读SharePoint网站。这是我已请求并获得以下管理员同意的权限:
目前,我已经设置了一个应用程序秘密,但我确实打算稍后再使用证书。
我有以下代码来获取访问令牌,并且确实获得了访问令牌:
const params = new URLSearchParams();
params.append("grant_type", "client_credentials");
params.append("scope", "https://graph.microsoft.com/.default");
params.append("client_id", process.env.client_id);
params.append("client_secret", process.env.client_secret);
var url = `https://login.microsoftonline.com/${tenant}/oauth2/v2.0/token`;
const response = await fetch(url,
{
method: 'POST',
body: params,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
}
);
但是,当我尝试阅读下面的根站点时
var url = "https://graph.microsoft.com/v1.0/sites?search=*";
const response = await fetch(url,
{
method: 'GET',
headers: { 'Authorization': `Bearer ${access_token}` }
}
);
我收到此错误:
error: {
code: 'AccessDenied',
message: 'Either scp or roles claim need to be present in the token.',
innerError: {
'request-id': 'ec47913f-2624-4d1c-9b27-5baf05ccebfd',
date: '2019-08-16T14: 15: 37'
}
}
我在https://jwt.io/处检查了令牌,实际上我没有看到roles
或scp
的任何条目。
我好像错过了一步,但是我无法弄清楚哪一步。
我正在获得这样的令牌:
https://login.microsoftonline.com/${tenant}/oauth2/v2.0/token
我做错了什么?
答案 0 :(得分:1)
首先要了解的是,您不能在同一令牌中同时收到“应用程序”和“委派”权限,这是一个“或/或”场景。接收哪种类型完全取决于您用于请求令牌的OAuth授权:
scp
属性的委托令牌roles
属性的应用程序令牌第二件事是您已请求两个不同API的作用域。根据所选内容,您将无法通过Microsoft Graph访问SharePoint,因为您仅请求访问旧版SharePoint API。更重要的是,您仅请求Graph的Delegated User.Read
范围,因此当您使用客户端凭据获取令牌时,该令牌将没有任何权限。
为了获得用于读取SharePoint网站的应用程序令牌,您需要选择Sites.Read.All
Microsoft Graph Application permission。