向共享点询问列表中的项目时发生 UnauthorizedAccessException
UnauthorizedAccessException when asking sharepoint for items in a list
我正在尝试访问我的共享点网站中的列表项。我在页面“https://XXXXXX.sharepoint.com/sites/XXXXX/_layouts/15/appregnew.aspx". Then I create the access rule on the page "https://XXXXX.sharepoint.com/sites/XXXXX/_layouts/15/appinv.aspx”上创建了一个 clientId 和一个 secretId。
在此页面上,我为访问规则设置了 XML 代码,例如:
<AppPermissionRequests><AppPermissionRequest
Scope="http://sharepoint/content/sitecollection"
Right="FullControl"/></AppPermissionRequests>
我创建规则并在下一页确认。
在我的应用程序中,我使用此代码获取令牌
let payload = `grant_type=client_credentials
&client_id=${clientId}@${tenantId}
&client_secret=${clientSecret}
&resource=${resource}/${domain}@${tenantId}`;
let authOptions = {
hostname: 'accounts.accesscontrol.windows.net',
path: `/${tenantId}/tokens/OAuth/2`,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(payload)
}
};
let req = https.request(authOptions, res => {...})
然后我这样做以获取列表中的项目:
let listOptions = {
hostname: 'XXXXX.sharepoint.com',
path: '/sites/XXX/_api/lists/getbytitle('MyList')/items',
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': `bearer ${accessToken}`,
'Accept': 'application/json'
}
};
let req = https.request(listOptions, res => {
res.setEncoding('utf8');
let stringResult = '';
res.on('data', chunk => {
stringResult += chunk.toString();
});
Sharepoint 总是return这个错误
"{"error":{"code":"-2147024891, System.UnauthorizedAccessException","message":{"lang":"en-US","value":"Access denied. You do not have permission to perform this action or access this resource."}}}"
我终于找到它不起作用的原因了。我需要添加 AllowAppOnlyPolicy :
<AppPermissionRequests AllowAppOnlyPolicy="true">
<AppPermissionRequest Scope="http://sharepoint/content/sitecollection" Right="FullControl" />
我正在尝试访问我的共享点网站中的列表项。我在页面“https://XXXXXX.sharepoint.com/sites/XXXXX/_layouts/15/appregnew.aspx". Then I create the access rule on the page "https://XXXXX.sharepoint.com/sites/XXXXX/_layouts/15/appinv.aspx”上创建了一个 clientId 和一个 secretId。 在此页面上,我为访问规则设置了 XML 代码,例如:
<AppPermissionRequests><AppPermissionRequest
Scope="http://sharepoint/content/sitecollection"
Right="FullControl"/></AppPermissionRequests>
我创建规则并在下一页确认。
在我的应用程序中,我使用此代码获取令牌
let payload = `grant_type=client_credentials
&client_id=${clientId}@${tenantId}
&client_secret=${clientSecret}
&resource=${resource}/${domain}@${tenantId}`;
let authOptions = {
hostname: 'accounts.accesscontrol.windows.net',
path: `/${tenantId}/tokens/OAuth/2`,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(payload)
}
};
let req = https.request(authOptions, res => {...})
然后我这样做以获取列表中的项目:
let listOptions = {
hostname: 'XXXXX.sharepoint.com',
path: '/sites/XXX/_api/lists/getbytitle('MyList')/items',
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': `bearer ${accessToken}`,
'Accept': 'application/json'
}
};
let req = https.request(listOptions, res => {
res.setEncoding('utf8');
let stringResult = '';
res.on('data', chunk => {
stringResult += chunk.toString();
});
Sharepoint 总是return这个错误
"{"error":{"code":"-2147024891, System.UnauthorizedAccessException","message":{"lang":"en-US","value":"Access denied. You do not have permission to perform this action or access this resource."}}}"
我终于找到它不起作用的原因了。我需要添加 AllowAppOnlyPolicy :
<AppPermissionRequests AllowAppOnlyPolicy="true">
<AppPermissionRequest Scope="http://sharepoint/content/sitecollection" Right="FullControl" />