我需要连接到我们的公司PWA。这是我正在使用的代码:
// var endpointUrl = 'https://<companySite>.sharepoint.com/sites/pwa/_api/web/lists';
var endpointUrl = 'https://<companySite>.sharepoint.com/sites/pwa/_api/ProjectData/Projects?$select=ProjectName';
var xhr = new XMLHttpRequest();
xhr.open("GET", endpointUrl);
// The APIs require an OAuth access token in the Authorization header, formatted like this: 'Authorization: Bearer <token>'.
xhr.setRequestHeader("Authorization", "Bearer " + token);
xhr.setRequestHeader("Accept", "application/json");
$("#header").html("Requesting: " + endpointUrl);
// Process the response from the API.
xhr.onload = function () {
if (xhr.status == 200) {
var formattedResponse = JSON.stringify(JSON.parse(xhr.response), undefined, 2);
$("#results").html("<pre>" + formattedResponse + "</pre>");
} else {
$("#results").html("HTTP " + xhr.status + "<br>" + xhr.response);
}
}
// Make request.
xhr.send();
我也尝试了几种不同的方式,都使用了Bearer令牌。
问题是此代码适用于访问https://<companySite>.sharepoint.com/sites/pwa/_api/web/lists
,但不适用于https://<companySite>.sharepoint.com/sites/pwa/_api/ProjectData/Projects?$select=ProjectName
对于后者,它返回:
{"odata.error":{"code":"20010, Microsoft.ProjectServer.PJClientCallableException","message":{"lang":"en-US","value":"GeneralSecurityAccessDenied"}}}
可能出现什么问题?
我知道我的令牌是正确的,因为它适用于访问*/web/lists
。我也知道网址是正确的,因为我可以在浏览器中打开它(假设我已登录到sharepoint)
答案 0 :(得分:0)
您需要使用FormDigestValue。
对... / _ api / contextinfo进行GET调用并存储'FormDigestValue'的值。然后,对于所有其他来电,请添加X-RequestDigest: <FormDigestValue>