Sean Ryan Sean Ryan - 2 months ago 31
reST (reStructuredText) Question

Dropbox Request URL path to file C#

I have OneDrive & Google Drive successfully processing chunked download however Dropbox is giving me grief because I cannot get the correct http request path to the file.

I am not an expert in rest url's & endpoints, maybe someone can point me in the right direction for the acceptable dropbox request format for the latest UWP SDK.

using (var httpRequest = new HttpRequestMessage())
{
string url = "https://content.dropboxapi.com/1/files/auto" + uri;

string accessKey = ApplicationData.Current.LocalSettings.Values[CommonData.dropboxAccessToken_Key].ToString();

httpRequest.Method = HttpMethod.Get;
httpRequest.RequestUri = new Uri(url);
httpRequest.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("bearer", accessKey);
}


I have read docs on Dropbox and it is not clear on the formatting for me, also I could not find a clear example anywhere.

Thanks again!

Answer

According to your code, the problem here is in your authorization header. For Dropbox API, the correct authorization header should like following:

Authorization: Bearer <access token>

So we should change httpRequest.Headers.Authorization to

httpRequest.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);

Then your code should be albe to work. Using "file.mp3" under "temp" folder for example.
enter image description here

The code may like:

var uri = "/temp/file.mp3";
using (var httpClient = new HttpClient())
{
    using (var httpRequest = new HttpRequestMessage())
    {
        string url = "https://content.dropboxapi.com/1/files/auto" + Uri.EscapeDataString(uri);

        httpRequest.Method = HttpMethod.Get;
        httpRequest.RequestUri = new Uri(url);
        httpRequest.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);

        var response = await httpClient.SendAsync(httpRequest);

        if (response.IsSuccessStatusCode)
        {
            //TODO
        }
    }
}