我在通过 Microsoft 的 REST API(或至少他们这么调用它)为 Office 365 上运行的 Sharepoint 创建/上传文件时遇到问题。看起来我能够顺利进行身份验证,但我遇到了问题当我尝试创建文件时出现403 Forbidden。同一用户可以使用该网站上传文件。
我一直在使用的代码可以在 http://jsfiddle.net/Lw8hcyda/5/ 上看到。 (请注意,如果您尝试在浏览器中运行它,则需要允许跨域请求。)
$.ajax({
url: 'https://examplecustomer.sharepoint.com/sites/examplesite/_api/web/GetFolderByServerRelativeUrl(\'/sites/examplesite/Documents/images\')/Files/add(url=\'testing-rest.txt\',overwrite=true)',
type: 'POST',
data: 'contents',
headers: {
'X-RequestDigest': digest
},
success: function (data, textStatus, jqXhr) {
console.log('File created. :-D');
},
error: function (jqXhr, textStatus, errorThrown) {
console.log('Failed to create file. Got status [' + textStatus + '] and error [' + errorThrown + '].');
}
});
使用GET列出文件到https://examplecustomer.sharepoint.com/sites/examplesite/_api/web/GetFolderByServerRelativeUrl('/sites/examplesite/Documents/images')/Files
100% 工作(但这不需要请求摘要)。
使用 POST 向 https://examplecustomer.sharepoint.com/sites/examplesite/_api/contextinfo
获取新请求摘要也会失败,并显示 403 Forbidden 。
我有一个看起来有效的 X-RequestDigest
(来自登录后返回的页面),并且我获得了 FedAuth
和 rtFa< 的值
cookie。
我发现的使用服务的大部分帮助来自互联网上的各种博客文章。在评论中,通常有一些讲述了相同的问题,尽管我还没有看到任何解决方案。
最佳答案
正如 JakobN 和 JoyS 在评论中指出的那样:
改变:
headers: {
'X-RequestDigest': digest
},
致:
headers: {
'Authorization': 'Bearer '+digest
},
帮我搞定了! Not the first SPO 身份验证突然发生的变化让我感到困惑。
关于javascript - 无法通过 REST 将文件上传到 Sharepoint @ Office 365,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30980272/