Forum Discussion

Jenya Korolenko's avatar
Jenya Korolenko
Explorer | Level 3
5 years ago

Can't get access token uses javascript fetch request

Hi everyone, l try to get access token uses javascript fetch in the next way:

method: 'POST',
code: AUTHORIZATION_CODE,
grant_type: 'authorization_code',
redirect_uri: `${REDIRECT_URI}${window.location.pathname}`,
headers: {
'Content-Type': "application/x-www-form-urlencoded"
},
CLIENT_ID: CLIENT_SECRET
})
.then(res => res.json())
.then(data => console.log('access data =>', data))
.catch(err => console.log('access err =>',err));

I receive AUTHORIZATION_CODE after successful redirecting from 
"https://www.dropbox.com/oauth2/authorize..." domain.

But I have an err like
{"error_description": "No auth function available for given request", "error": "invalid_request"}

What am I doing wrong? Because I follow curl request example from here:
https://www.dropbox.com/developers/documentation/http/documentation#oauth2-token

curl https://api.dropbox.com/oauth2/token \
    -d code=<AUTHORIZATION_CODE> \
    -d grant_type=authorization_code \
    -d redirect_uri=<REDIRECT_URI> \
    -u <APP_KEY>:<APP_SECRET>
Thanks!

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox Staff

    [Cross-linking for reference: https://stackoverflow.com/questions/64178976/cant-get-access-token-in-dropbox-uses-javascript-fetch-request ]

     

    It looks like there's a few issues with how you're formatting your request. For instance, it looks like you're not using the right encoding, and you're not properly labeling your client ID and secret. Here's a fixed up example of how you can do this:

    const params = new URLSearchParams({
        code: AUTHORIZATION_CODE,
        grant_type: 'authorization_code',
        redirect_uri: REDIRECT_URI,
        client_id: CLIENT_ID,
        client_secret: CLIENT_SECRET
    });
    
    fetch('https://api.dropboxapi.com/oauth2/token', {
        method: 'POST',
        body: params
    })
    .then(res => res.json())
    .then(data => console.log('access data =>', data))
    .catch(err => console.log('access err =>',err));
      • Greg-DB's avatar
        Greg-DB
        Icon for Dropbox Staff rankDropbox Staff

        cascadeCommons The /2/file_requests/create endpoint is an "RPC" style endpoint, so it does work a bit differently than the /oauth2/token endpoint. 

         

        Calling /2/file_requests/create with fetch, for instance, would look like this, again building on the original example here:

        var headers = {
            'Authorization': "Bearer ACCESS_TOKEN",
            'Content-Type': "application/json"
        };
        var params = JSON.stringify({"title": "file request title", "destination": "/file request destination"});
        
        fetch('https://api.dropboxapi.com/2/file_requests/create', {
            method: 'POST',
            body: params,
            headers: headers,
        })
        .then(res => res.json())
        .then(data => console.log('access data =>', data))
        .catch(err => console.log('access err =>', err));

         

         

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.5,949 PostsLatest Activity: 12 minutes ago
352 Following

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!