I have generated an access token using OAuth mechanism for SharePoint Online server. I am using this token to create ClientContext using CSOM. While I am able to access all the sites, libraries, and folders seamlessly, I get error

The remote server returned an error: (401) Unauthorized.

while downloading the file from SharePoint Online. Below is the code that I am using for file download:

    var clientContext = TokenHelper.GetClientContextWithAccessToken("https://adventurer.sharepoint.com/Subsite1", accessToken);
    var list = clientContext.Web.Lists.GetByTitle("SubSite 1 Library 1");
    string vquery = @"<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='UniqueId' /><Value Type='Lookup'>" + "6718053d-a785-489c-877f-5a4b88dcb2a7" + "</Value></Eq></Where></Query></View>";
    CamlQuery query = new CamlQuery();
    query.ViewXml = vquery;
    var listItems = list.GetItems(query);
    clientContext.Load(listItems, items => items.Take(1).Include(item => item.File));

    var fileRef = listItems[0].File.ServerRelativeUrl;
    var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, fileRef);

I don't understand the root cause of this error, as I am passing client context with right access token. I want to know if OpenBinaryDirect has a limitation to work with access tokens? If not, what is wrong with above code? Is there any other alternative that can be used to download using access token?

  • Unrelated to this question, but I have a similar requirement to access Sharepoint 2013 environment through access tokens. Can you please share the code for generating the access token for a particular user to pass it further in client context Jun 5, 2018 at 11:36

1 Answer 1


Looks like an issue downloading file using Access token.

As a workaround, you can use the OpenBinaryStream method as below:

var listItems = list.GetItems(query);
clientContext.Load(listItems, items => items.Take(1).Include(item => item.File));

Microsoft.SharePoint.Client.File file = listItems[0].File;
ClientResult<Stream> data = file.OpenBinaryStream();

// Load the Stream data for the file

MemoryStream memStream = new MemoryStream();

//change the path as per your requirement
string downloadedFile = @"C:\Users\Admin\Downloads\yourfile.name";


Have tested the above code at my end seems to work for almost all major file formats(png, jpg ,docx,pdf etc.) .

Modified from - SharePoint 2013 Online App: 403 Response While Downloading Documents from a Document Library

  • I could use OpenBinaryStream() instead of OpenBinaryDirect(), but the issue is, we are using Microsoft.SharePoint.Client.dll, v14.0.0.0 in our project, and this version doesn't have OpenBinaryStream(). I think this method has been added from after v14.0.0.0. Aug 16, 2017 at 5:26
  • Hmm it could be, but if you are in SPO or even 2013, you can use the v15.0.0.0 dll. It should be fine. Aug 16, 2017 at 5:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.