2

I'm just starting out with APIs, and full disclosure... I copy and pasted from this answer: How do I make calls to a REST api using c#? But clearly I'm pretty much lost.

I'm trying to start simple by just getting info from the dropbox api.

I plugged in the api URL but I cannot figure out how to pass in the authentication token or the account ID.

private const string URL = "https://api.dropboxapi.com/2-beta-2/users/get_current_account";
        private const string urlParameters = "access_token=xxxxxxxxxxxxx";

        static void Main(string[] args)
        {
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri(URL);

            // Add an Accept header for JSON format.
            client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));

            // List data response.
            HttpResponseMessage response = client.GetAsync(urlParameters).Result;  // Blocking call!
            if (response.IsSuccessStatusCode)
            {
                // Parse the response body. Blocking!
                var dataObjects = response.Content.ReadAsAsync<IEnumerable<DataObject>>().Result;
                foreach (var d in dataObjects)
                {
                    Console.WriteLine("{0}", d.Name);
                    Console.ReadKey();
                }
            }

from Dropbox the http request should be:

POST /2-beta-2/users/get_account
Host: https://api.dropboxapi.com
User-Agent: api-explorer-client
Authorization: Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxx
Content-Type: application/json

{
    "account_id": "dbid:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
Community
  • 1
  • 1
Matt Winer
  • 495
  • 9
  • 26
  • You should consider using HttpWebRequest. Here is an example: http://www.codeproject.com/Articles/18034/HttpWebRequest-Response-in-a-Nutshell-Part -- also check this out for more info: http://stackoverflow.com/questions/4988286/what-difference-is-there-between-webclient-and-httpwebrequest-classes-in-net – Glenn Ferrie Oct 15 '15 at 18:15

1 Answers1

0

You probably need to add the Bearer token to the Headers in the request

client.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);
Wil Wang
  • 74
  • 3