简体   繁体   English

无法通过REST将文件上传到Sharepoint @ Office 365

[英]Unable to upload file to Sharepoint @ Office 365 via REST

I'm having trouble creating/uploading files via Microsoft's REST API (or at least that's what they call it) for Sharepoint running on Office 365. It looks like I'm able to authenticate all right, but I'm getting 403 Forbidden when I try to create a file. 对于在Office 365上运行的Sharepoint,我无法通过Microsoft的REST API(或至少是他们所谓的名称)创建/上传文件。看起来我可以进行所有身份验证,但是当出现403 Forbidden时我尝试创建一个文件。 The same user can upload a file using the website. 同一用户可以使用该网站上载文件。

The code I've been using can be seen on http://jsfiddle.net/Lw8hcyda/5/ . 我一直在使用的代码可以在http://jsfiddle.net/Lw8hcyda/5/上看到。 (Please note that you need to allow cross domain requests if you try to run it in your browser.) (请注意,如果尝试在浏览器中运行跨域请求,则需要允许跨域请求。)

    $.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 + '].');
        }
    });

Listing files using GET to https://examplecustomer.sharepoint.com/sites/examplesite/_api/web/GetFolderByServerRelativeUrl('/sites/examplesite/Documents/images')/Files works 100% (but that doesn't require that request digest). 使用GET列出文件到https://examplecustomer.sharepoint.com/sites/examplesite/_api/web/GetFolderByServerRelativeUrl('/sites/examplesite/Documents/images')/Files文件可以100%工作(但这不需要该请求)消化)。

Getting a new request digest using POST to https://examplecustomer.sharepoint.com/sites/examplesite/_api/contextinfo also fails with 403 Forbidden . 使用POST获取新的请求摘要到https://examplecustomer.sharepoint.com/sites/examplesite/_api/contextinfo也会失败,并显示403 Forbidden

I've got a X-RequestDigest (from the page returned after logging in) that seems valid and I got values for the FedAuth and rtFa cookies. 我有一个似乎有效的X-RequestDigest (从登录后返回的页面),并且我获得了FedAuthrtFa cookie的值。

Most of the help for using the services I've found have been various blog posts from around the Internet. 使用我发现的服务的大多数帮助来自Internet上的各种博客文章。 In the comments there are typically a few that tell about the same problem, though I haven't seen any solutions. 尽管我还没有看到任何解决方案,但是在评论中通常有一些讲述相同的问题。

To me it just seems a bit light, visit this link https://msdn.microsoft.com/en-us/library/office/dn769086.aspx on technet, and compare your upload function to this: 对我来说,似乎有点轻松,请在technet上访问以下链接https://msdn.microsoft.com/zh-cn/library/office/dn769086.aspx ,然后将您的上传功能与此进行比较:

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

Also; 也; since calling office online from a mobile app, you should include the Authorization header "Bearer <>" 由于通过移动应用程序在线致电办公室,因此您应包括授权标头“ Bearer <>”

As JakobN and JoyS Points out in the comments: 正如JakobN和JoyS在评论中指出的那样:

Changing: 变更:

headers: {
    'X-RequestDigest': digest
},

To: 至:

headers: {
    'Authorization': 'Bearer '+digest
},

Did the trick for me! 为我做了把戏! Not the first SPO authentication sudden change that has thrown me off. 不是第一个 SPO身份验证突然发生的变化让我失望。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM