ajax获取Office365 REST Api请求失败CORS?

Jef*_*man 8 ajax cors office365

我正在尝试从本地服务器向Office365 RESTful API服务发出ajax GET请求,但遇到了跨域HTTPRequest错误.以下是我的'get-files-at-root'尝试的示例:

$.ajax({
  url: 'https://[sharepoint_site]/_api/v1.0/me/files?access_token='+token,
  type: 'get',
  dataType: 'json',
  success: function(data) {
    if (success){
      success(data);
    }
  },
  error: error
})
Run Code Online (Sandbox Code Playgroud)

我从服务器得到以下响应:

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 403.
Run Code Online (Sandbox Code Playgroud)

我已经尝试将访问令牌作为标头参数发送:

headers: {'Authorization': 'Bearer '+ token}
Run Code Online (Sandbox Code Playgroud)

但这也有同样的结果.

关于我做错了什么的任何想法?

(背景:我正在尝试在客户端上创建自己的Office365'文件选择器',因为我找不到提供此功能的OneDrive Business的可用库.)

小智 4

Office 365 文件 API 和 SharePoint REST 刚刚引入了对 CORS 的支持。

https://msdn.microsoft.com/en-us/office/office365/howto/create-web-apps-using-CORS-to-access-files-in-Office-365

你想要做的正是它的工作原理。该服务将使用 Access-Control-Allow-Origin 标头响应 OPTIONS 飞行前请求。

请求中的授权必须是 Azure Active Directory 颁发的 OAuth2 隐式授予访问令牌。