cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Back again with another edition of 'How we use Dropbox', so find out how Emma uses to-do lists to get it all done here!

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

how download file with oauth2

how download file with oauth2

Daniele F.1
New member | Level 1

Hi all, I'm switching my Dropbox app from oauth1 to oauth2. I've an issue when I try to download a file.

I use this code:

var r = Call("https://content.dropboxapi.com/2/files/download", access_token, "{\"path\":\"" + metaPath + "\"}");

 

private string Call(string endpoint, string access_token, string json) {
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endpoint);
request.ContentType = "application/json";
request.Headers.Add("Authorization", "Bearer " + access_token);
request.Method = "POST";
Stream requestStream = request.GetRequestStream();
requestStream.Write(Encoding.UTF8.GetBytes(json), 0, json.Length);
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string txtResponse = reader.ReadToEnd();
return txtResponse;
}

 

The "Call" function works well whit *list_folder* endpoint. But I get a 400 http error (invalid request) with *download* endopint. Any tips? It's the right way?

Thanks!

2 Replies 2

Re: how download file with oauth2

Steve M.
Dropboxer

Sorry for the confusion here... we're still missing some overview documentation that may have made this clearer.

For "content-style" endpoints like upload and download, the JSON is passed in the "Dropbox-Api-Arg" header, not in the body of the request. Take a look at the example curl requests in the documentation to get the hang of this.

So you'll want something like this (completely untested):

private string CallContent(string endpoint, string access_token, string json) {
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endpoint);
request.Headers.Add("Authorization", "Bearer " + access_token);
request.Headers.Add("Dropbox-API-Arg", json);
request.Method = "POST";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string txtResponse = reader.ReadToEnd();
return txtResponse;
}

Re: how download file with oauth2

Steve M.
Dropboxer

As another tip, I'd say you should always read the body of the HTTP response when you get an error. The body of the 400 from the API will generally tell you exactly what's wrong with the request.

Poll
We love to learn from the educators who use Dropbox. Whether you teach kids, teens, adults or a combination of all three, we want to know what apps and integrations you use with Dropbox to help with teaching. Which of the ones below is your favorite, or most used tool?
Who's talking

Top contributors to this post

  • User avatar
    Steve M. Dropboxer
What do Dropbox user levels mean?
Need more support?