Download file from SharePoint

2019-04-13 16:42发布

问题:

I'm using the REST APIs of SharePoint to query the files hosted inside a corporate user's Office 365 SharePoint site.

I have used OAuth to authenticate the user and authorize the the app, as described here: http://msdn.microsoft.com/en-us/library/azure/dn645542.aspx

I am able to successfully query the API with a GET request on: https://XXXXXXX.sharepoint.com/sites/xxxxx/xxxxx/myfoldername/_api/files (the request as the Authorization header set to 'Bearer ', as you'd expect.)

The above query gives me the following JSON result:

{
    "odata.metadata" = "https://xxxxxxxxx.sharepoint.com/sites/xxxxxxxxx/xxxxxxxxx/myfoldername/_api/$metadata#SP.ApiData.Files";
    value =     ( 
                {
            CreatedBy =             {
                Id = 1;
                Name = “Test”;
                Puid = xxxxx;
            };
            ETag = “\”{hsdglksjfldkasnfldasknk},4\””;
            Id = JKLBbiuuu908yYHhh89YBn9n9ynynYUN;
            LastModifiedBy =             {
                Id = 1;
                Name = “XXXXX”;
                Puid = XXXXXXXXXXX;
            };
            Name = "videofile.wmv";
            Size = 56492358;
            TimeCreated = "2013-09-23T05:16:44Z";
            TimeLastModified = "2013-09-23T05:28:42Z";
            Url = "https://xxxxxxxxx.sharepoint.com/sites/xxxxxxxxx/xxxxxxxxx/myfoldername/Shared Documents/videofile.wmv";
            "odata.editLink" = "Web/Lists(guid’9568472598-2c28-489BF2-a1ae-kfjdasfkjb)/files(‘jkbvasdjfbakfndasnMNBBAMnsaldfan)”;
            "odata.id" = "https://xxxxxxxxx.sharepoint.com/sites/xxxxxxxxx/xxxxxxxxx/myfoldername/_api/Web/Lists(guid’987795-gfkjhghs-ty42398yhfo’)/files('jkbvasdjfbakfndasnMNBBAMnsaldfan')";
            "odata.type" = "MS.FileServices.File";
        })
}

(Bear in mind the above is not actually JSON, but text output from a JSON parser)

In the result above, I get an array of files inside the value part. I have cut this array down to one item for illustrative purposes.

You'll see each item represents a file's meta data. There's a property called Url; I then use this to attempt to download the file. I create an HTTP request, append the Authorization header with Bearer <access_token>, however, the response is always an HTTP 401 UNAUTHORIZED.

Please note: the software I'm writing is actually in Objective-C using AFNetworking 2's AFHTTPRequestOperationManager.

Does anyone know how I can actually download the file? I have seen references to some property called 'contentUrl' in the docs, but this property is not supplied.

thanks Kris

回答1:

Are you using the v1 of the API? From your endpoint it seems like you're using preview (v1 was released 2 weeks ago; we might still have outdated references on our documentation pages, please let us know if you come across any)

Example:

GET https://{tenant}.sharepoint.com/{site-path}/_api/v1.0/Files/{file-id}/content

This is the URL of the documentation section that shows you how to download a file: http://msdn.microsoft.com/en-us/office/office365/api/files-rest-operations#FileresourceDownloadafile

Let me know if you have any other issues.