从单独的应用程序访问 Sharepoint Project Web 应用程序 REST [英] Accessing Sharepoint Project web app REST from separate app

查看:62
本文介绍了从单独的应用程序访问 Sharepoint Project Web 应用程序 REST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要连接到我们公司的 PWA.这是我正在使用的代码:

//var endpointUrl = 'https://.sharepoint.com/sites/pwa/_api/web/lists';var endpointUrl = 'https://.sharepoint.com/sites/pwa/_api/ProjectData/Projects?$select=ProjectName';var xhr = new XMLHttpRequest();xhr.open("GET", endpointUrl);//API 需要授权标头中的 OAuth 访问令牌,格式如下:'Authorization: Bearer '.xhr.setRequestHeader("Authorization", "Bearer" + token);xhr.setRequestHeader("Accept", "application/json");$("#header").html("请求:" + endpointUrl);//处理来自 API 的响应.xhr.onload = 函数 () {如果(xhr.status == 200){var formattedResponse = JSON.stringify(JSON.parse(xhr.response), undefined, 2);$("#results").html("

" + formattedResponse + "

");} 别的 {$("#results").html("HTTP" + xhr.status + "
" + xhr.response);}}//发出请求.xhr.send();

我也尝试了几种不同的方法,都使用不记名令牌.

问题是此代码适用于访问 https://.sharepoint.com/sites/pwa/_api/web/lists 但不适用于 https://.sharepoint.com/sites/pwa/_api/ProjectData/Projects?$select=ProjectName

对于后者,它返回:

{"odata.error":{"code":"20010, Microsoft.ProjectServer.PJClientCallableException","message":{"lang":"en-US","value":"GeneralSecurityAccessDenied"}}}

可能的问题是什么?

我知道我的令牌是正确的,因为它适用于访问 */web/lists.我也知道 url 是正确的,因为我可以在浏览器中打开它(前提是我已登录到 sharepoint)

解决方案

您需要使用 FormDigestValue.

对 .../_api/contextinfo 进行 GET 调用并存储FormDigestValue"的值.然后对于所有其他调用,添加 X-RequestDigest 的标头:<FormDigestValue>

I need to connect to our corporate PWA. This is the code I'm using:

// 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();

I've tried also a few different ways, all using Bearer token.

The problem is that this code works for accessing https://<companySite>.sharepoint.com/sites/pwa/_api/web/lists but doesn't for https://<companySite>.sharepoint.com/sites/pwa/_api/ProjectData/Projects?$select=ProjectName

For the latter it returns:

{"odata.error":{"code":"20010, Microsoft.ProjectServer.PJClientCallableException","message":{"lang":"en-US","value":"GeneralSecurityAccessDenied"}}} 

What could be the possible problem?

I know that my token is correct, as it works for accessing */web/lists. I also know that the url is correct, as I can open it in my browser (providing that I'm logged in into sharepoint)

解决方案

You need to use a FormDigestValue.

Make a GET call to .../_api/contextinfo and store the value of 'FormDigestValue'. Then for all your other calls, add a header of X-RequestDigest: <FormDigestValue>

这篇关于从单独的应用程序访问 Sharepoint Project Web 应用程序 REST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆