cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Are you new to Dropbox? Our Newcomers group is the perfect place to learn the basics, find out more here.

Discuss Dropbox Developer & API

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

Get Access Token JavaScript in fetch

Get Access Token JavaScript in fetch

Fomka_Wyverno
New member | Level 2

 

 

            const parameters = {
                'code': params.code,
                'grant_type': 'authorization_code',
                'redirect_uri': window.location.origin,
                'client_id': dataApp.client_id,
                'client_secret': dataApp.client_secret
            }


            console.log(parameters);

            fetch('https://api.dropbox.com/oauth2/token', {
                method: 'POST',
                body: JSON.stringify(parameters)
            }).then(response => console.log(response))
                .catch(error => console.log(`Error in POST: ${error}`));

 

 

 

params.code - this is code when i first redirect to my http in url parameters
window.location.origin - origin url my web-site
dataApp.client_id - Api Key my application
dataApp.client_secret - App secret my application
But when fetching my response 400 - why?

I looked at the curl convenerator in js suggested to do this

 

 

fetch('https://api.dropbox.com/oauth2/token', {
  method: 'POST',
  body: new URLSearchParams({
    'code': params.code,
    'grant_type': 'authorization_code',
    'redirect_uri':  window.location.origin,
    'client_id': dataApp.client_id,
    'client_secret': dataApp.client_secret
  })
});

 

 

But this again not work

GHX7IlZzgqvlbw
1 Accepted Solution

Accepted Solutions

Greg-DB
Dropbox Staff

It looks like you're sending the parameters as JSON, but the /oauth2/token endpoint expects them as application/x-www-form-urlencoded POST parameters, and so your parameters aren't getting read. Update your implementation to send application/x-www-form-urlencoded POST parameters instead.

View solution in original post

3 Replies 3

Greg-DB
Dropbox Staff

Check the contents of the response body for that error response. It should contain a more specific error message.

Fomka_Wyverno
New member | Level 2

ftbbQaW

The request parameters do not match any of the supported authorization flows. Please refer to the API documentation for the correct parameters.


but I don't understand why if I used all the parameters from the documentation. At the same time, if you do it in сURL, everything will work out well

Greg-DB
Dropbox Staff

It looks like you're sending the parameters as JSON, but the /oauth2/token endpoint expects them as application/x-www-form-urlencoded POST parameters, and so your parameters aren't getting read. Update your implementation to send application/x-www-form-urlencoded POST parameters instead.

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Greg-DB Dropbox Staff
  • User avatar
    Fomka_Wyverno New member | Level 2
What do Dropbox user levels mean?