Search code examples
curlparse-platformdropbox-apiparse-cloud-code

Could not access Dropbox API via parse cloud code although works with CURL


I am trying to access the following end-point from parse cloud code:

https://api.dropboxapi.com/2/users/get_current_account

Details of end-point:

https://www.dropbox.com/developers/documentation/http/documentation#users-get_current_account

My cloud function at the point of making request.

return Parse.Cloud.httpRequest({
  method: 'POST',
  url: 'https://api.dropboxapi.com/2/users/get_current_account',
  headers: {
    'Authorization': 'Bearer ' + accessToken
  }
});

Error received from Dropbox:

Error in call to API function "users/get_current_account": Bad HTTP "Content-Type" header: "application/x-www-form-urlencoded".  Expecting one of "application/json", "application/json; charset=utf-8", "text/plain; charset=dropbox-cors-hack"

I then tried using CURL:

curl -X POST https://api.dropboxapi.com/2/users/get_current_account --header "Authorization: Bearer **access_token"

This worked and I got the user's data.

Based on the error, I modified the cloud code:

return Parse.Cloud.httpRequest({
  method: 'POST',
  url: 'https://api.dropboxapi.com/2/users/get_current_account',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer ' + newAccessToken
  }
});

Now I got the following error:

Error in call to API function \"users/get_current_account\": request body: could not decode input as JSON

There is no request body in the httpRequest. I then tried setting the body to null and ''. Both gave the same error.

First, I don't understand why the dropbox API requires a POST request with no parameters.

Second, how could I resolve the error.

Many Thanks!

----- Update -----

Have tried the other two content types from the error i.e.

'Content-Type': 'application/json;charset=utf-8'
'Content-Type': 'text/plain; charset=dropbox-cors-hack'

and still get the same error:

Error in call to API function \"users/get_current_account\": request body: could not decode input as JSON

Solution

  • Finally! this worked for me:

    return Parse.Cloud.httpRequest({
      method: 'POST',
      url: 'https://api.dropboxapi.com/2/users/get_current_account',
      headers: {
        'Content-Type': 'application/json; charset=utf-8',
        'Authorization': 'Bearer ' + newAccessToken
      },
      body: JSON.stringify(null)
    });
    

    I had to use JSON.stringify(null) in the body.

    The link below helped me in identifying the solution:

    https://www.dropboxforum.com/hc/en-us/community/posts/204379696-get-current-account-API-is-strange-and-does-not-work-with-common-sense-