2

I am trying to use the Dropbox API get_latest_cursor and the documentation-explorer states.

For curl I would use the following:

POST /2/files/list_folder/get_latest_cursor
Host: https://api.dropboxapi.com
User-Agent: api-explorer-client
Authorization: Bearer oDNxxxxxxxxxxxxxxxxxxxxxxxxxxetc
Content-Type: application/json

{
    "path": "/Apps/Manchester"
}

My code, as I am working in .NET not curl, is:

public ActionResult GetCursor()
{
    string accessToken = "oDNxxxxxxxxxxxxxxxxxxxxxxxxxxetc";
    var request = (HttpWebRequest)WebRequest.Create("https://api.dropboxapi.com/2/files/get_latest_cursor");

    request.Method = "Post";
    request.Headers.Add("Authorization", "Bearer " + accessToken);
    request.ContentType = "application/json";

    //Build the data content
    ASCIIEncoding encoding = new ASCIIEncoding();
    string stringData = "{\"path\":\"/Apps/Manchester\"}";
    byte[] data = encoding.GetBytes(stringData);
    request.ContentLength = data.Length;
    Stream newStream = request.GetRequestStream();
    newStream.Write(data, 0, data.Length);
    newStream.Close();

    var response = (HttpWebResponse)request.GetResponse();

    StreamReader sr = new StreamReader(response.GetResponseStream());
    string responseString = sr.ReadToEnd();

    ViewBag.Json = responseString;

    return View();
}

I assume I have to add the JSON into the content body as I have done above.

I get the following error:

The remote server returned an error: (400) Bad Request

If I use the Dropbox API Explorer with the above path I get a cursor JSON value returned fine.

Can anyone point out the error of my ways please?


Server Error in '/SurveyDataManager' Application.

The remote server returned an error: (400) Bad Request. 
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

 Exception Details: System.Net.WebException: The remote server returned an error: (400) Bad Request.

Source Error: 



Line 61:             newStream.Close();
Line 62:             
Line 63:             var _response = (HttpWebResponse)_request.GetResponse();
Line 64: 
Line 65:             StreamReader sr = new StreamReader(_response.GetResponseStream());


 Source File:  d:\Development\SurveyDataManager\SurveyDataManager\Controllers\DropboxAPIController.cs    Line:  63 

Stack Trace: 

[WebException: The remote server returned an error: (400) Bad Request.]
   System.Net.HttpWebRequest.GetResponse() +1740
   SurveyDataManager.Controllers.DropboxAPIController.GetCursor() in d:\Development\SurveyDataManager\SurveyDataManager\Controllers\DropboxAPIController.cs:63
   lambda_method(Closure , ControllerBase , Object[] ) +87
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +265
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +35
   System.Web.Mvc.Async.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41() +34
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +60
   System.Web.Mvc.Async.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() +77
   System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +371
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +60
   System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +32
   System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +185
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +53
   System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +24
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +22
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +68
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +22
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +53
   System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +39
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +22
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +53
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +399
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +137

_request was a typo, and I know that the error happens on the typo line but it really is not the issue.


Dropbox says the problem is an "unknown API".

It is not files/get_latest_cursor but list_folder/get_latest_cursor.

Ondrej Tucny
  • 27,626
  • 6
  • 70
  • 90
Tim
  • 21
  • 2
  • What's the body of the HTTP response from the server? Without that, it's pretty hard to debug. – user94559 Mar 24 '16 at 23:17
  • Also, your code seems to bounce back and forth between using `request` and `_request`. If that's not just a typo, that could be the problem. – user94559 Mar 25 '16 at 05:48
  • Thanks for the quick response. I have added the response detail as you suggested and have edited the variable name - it was just a typo. The actual code does use an _ for the variable name. I edited (badly) for this post for some strange reason I know not why. Any suggestions are appreciated. – Tim Mar 25 '16 at 11:46
  • Sorry for the confusion. I really mean the body of the HTTP response. You'll need to catch the WebException and read the response to see the error from the server. – user94559 Mar 25 '16 at 15:48
  • See http://stackoverflow.com/questions/11828843/webexception-how-to-get-whole-response-with-a-body if you're not sure how to do this. – user94559 Mar 25 '16 at 15:49
  • Thanks - that link was useful, didn't know about that method. I will update my post above following your suggestion – Tim Mar 25 '16 at 16:24
  • smarx how do you get credit here as I cannot accept a comment, only an answer? – Tim Mar 25 '16 at 16:38
  • @Tim It would be nice if smarx would add his answer so the answer could solve this problem of potential future visitors, but if he doesn't adds it, you can later [answer your own question](http://stackoverflow.com/help/self-answer) as well. – kayess Mar 25 '16 at 16:57
  • Thanks kayess - I will give smarx suitable time to do as you suggest and fill in for him if needed later. – Tim Mar 25 '16 at 17:02
  • Welcome to SO. Please read "[ask]" and "[mcve]". Formatting is extremely important as it helps us understand your question along with those who are searching for similar solutions in the future. Think of SO as a reference book in the making, and write accordingly, albeit a little less formally. It's not a discussion forum so questions and answers are not for discourse. Comments are suitable for asking questions. Include added information where it belongs in the flow of the text without using "edit" or "update" tags. – the Tin Man Mar 25 '16 at 17:11
  • Hi the Tin Man, have I done something wrong? If so what? – Tim Mar 26 '16 at 00:25

0 Answers0