Javascript 无法通过REST将文件上载到Sharepoint@Office 365
我无法通过运行在Office 365上的Microsoft的REST API(或至少是他们所说的)为Sharepoint创建/上载文件。看起来我可以正确地进行身份验证,但当我尝试创建文件时,403禁止。同一用户可以使用网站上传文件 我一直在使用的代码可以在上看到。(请注意,如果您尝试在浏览器中运行跨域请求,则需要允许跨域请求。) 使用GETtoJavascript 无法通过REST将文件上载到Sharepoint@Office 365,javascript,ajax,rest,sharepoint,office365,Javascript,Ajax,Rest,Sharepoint,Office365,我无法通过运行在Office 365上的Microsoft的REST API(或至少是他们所说的)为Sharepoint创建/上载文件。看起来我可以正确地进行身份验证,但当我尝试创建文件时,403禁止。同一用户可以使用网站上传文件 我一直在使用的代码可以在上看到。(请注意,如果您尝试在浏览器中运行跨域请求,则需要允许跨域请求。) 使用GETto列出文件https://examplecustomer.sharepoint.com/sites/examplesite/_api/web/GetFold
列出文件https://examplecustomer.sharepoint.com/sites/examplesite/_api/web/GetFolderByServerRelativeUrl('/sites/examplesite/Documents/images')/Files
100%有效(但不需要请求摘要)
使用POSTto获取新的请求摘要https://examplecustomer.sharepoint.com/sites/examplesite/_api/contextinfo
也会因403禁用而失败
我有一个X-RequestDigest
(来自登录后返回的页面)似乎有效,并且我得到了FedAuth
和rtFa
cookies的值
我发现使用这些服务的大部分帮助来自互联网上的各种博客帖子。虽然我还没有看到任何解决方案,但在评论中通常有一些是关于同一问题的。对我来说,这似乎有点轻,请访问technet上的此链接,并将您的上载功能与此进行比较:
// Add the file to the file collection in the Shared Documents folder.
function addFileToFolder(arrayBuffer) {
// Get the file name from the file input control on the page.
var parts = fileInput[0].value.split('\\');
var fileName = parts[parts.length - 1];
// Construct the endpoint.
var fileCollectionEndpoint = String.format(
"{0}/_api/sp.appcontextsite(@target)/web/getfolderbyserverrelativeurl('{1}')/files" +
"/add(overwrite=true, url='{2}')?@target='{3}'",
appWebUrl, serverRelativeUrlToFolder, fileName, hostWebUrl);
// Send the request and return the response.
// This call returns the SharePoint file.
return jQuery.ajax({
url: fileCollectionEndpoint,
type: "POST",
data: arrayBuffer,
processData: false,
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": jQuery("#__REQUESTDIGEST").val(),
"content-length": arrayBuffer.byteLength
}
});
}
还有,;由于通过移动应用程序呼叫office online,您应该包括授权标题“承载者”对我来说,这似乎有点轻,请访问technet上的此链接,并将您的上传功能与此进行比较:
// Add the file to the file collection in the Shared Documents folder.
function addFileToFolder(arrayBuffer) {
// Get the file name from the file input control on the page.
var parts = fileInput[0].value.split('\\');
var fileName = parts[parts.length - 1];
// Construct the endpoint.
var fileCollectionEndpoint = String.format(
"{0}/_api/sp.appcontextsite(@target)/web/getfolderbyserverrelativeurl('{1}')/files" +
"/add(overwrite=true, url='{2}')?@target='{3}'",
appWebUrl, serverRelativeUrlToFolder, fileName, hostWebUrl);
// Send the request and return the response.
// This call returns the SharePoint file.
return jQuery.ajax({
url: fileCollectionEndpoint,
type: "POST",
data: arrayBuffer,
processData: false,
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": jQuery("#__REQUESTDIGEST").val(),
"content-length": arrayBuffer.byteLength
}
});
}
还有,;由于通过移动应用程序在线呼叫office,您应该包括授权标题“承载者”,正如JakobN和JoyS在评论中指出的那样: 更改:
headers: {
'X-RequestDigest': digest
},
致:
你为我耍了把戏!SPO认证的突然变化让我感到厌烦。正如JakobN和JoyS在评论中指出的那样: 更改:
headers: {
'X-RequestDigest': digest
},
致:
你为我耍了把戏!SPO身份验证突然发生了变化,这让我很反感。从何处调用ajax?Sharepoint网站或Sharepoint托管应用程序中的页面?@Max:实际上,它来自一个iOS应用程序,在Web视图中显示本地文件(文件://)(使用Cordova)。另外,在桌面开发过程中,我通过file://和。这难道不应该起作用吗?在开发过程中,您使用过Internet Explorer,我想,尝试使用Chrome或Firefox进行桌面开发时,您会遇到错误。在桌面开发过程中,我使用Chrome并启用“-disable web security”标志。您从何处调用ajax?Sharepoint网站或Sharepoint托管应用程序中的页面?@Max:实际上,它来自一个iOS应用程序,在Web视图中显示本地文件(文件://)(使用Cordova)。另外,在桌面开发过程中,我通过file://和。这难道不应该起作用吗?在开发过程中,您使用过Internet Explorer,我想,尝试使用Chrome或Firefox进行桌面开发时,您会遇到错误。在桌面开发过程中,我使用Chrome并启用“-disable web security”标志。我不确定元素#uu REQUESTDIGEST是否存在,我想有必要再打一次电话来取回令牌。就是这样,@JakobN!我所要做的就是将“X-RequestDigest”标题改为“Authorization:Bearer[RequestDigest]”,这样我就可以上传文件了!您应该更新您的答案,以反映此处的真实问题,即缺少的授权标头。我不确定元素#uu REQUESTDIGEST是否存在,我认为有必要额外调用以检索令牌。就是它,@JakobN!我所要做的就是将“X-RequestDigest”标题改为“Authorization:Bearer[RequestDigest]”,这样我就可以上传文件了!您应该更新您的答案以反映此处的真正问题,即缺少授权标题。