cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

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

Help with generating access tokens in Node.js

Help with generating access tokens in Node.js

AndyLei
Explorer | Level 4
Go to solution

I am new to using https in Node.js, and Dropbox API, and am not familiar with the syntax. I have had no success finding Javascript examples on how to get an access token from a refresh token, and am asking here.

 

below is my current syntax, and i get the error:

{"error": "unsupported_grant_type", "error_description": "missing required field \"grant_type\""}

 

 

 

 

import * as https from "https";

const req = https.request("https://api.dropbox.com/oauth2/token",{
    method: "POST",
    headers:{
        "Authorization":`Basic ${Buffer.from(`${<APP_KEY>}:${<SECRET_KEY>}`).toString("base64")}`,
        "Content-Type":"application/x-www-form-urlencoded"
    }
}, (res) => {
    res.on("data", function(rawdat){
        console.log(rawdat.toString());
    })
});
req.write(JSON.stringify({
    grant_type:"refresh_token",
    refresh_token: <REFRESH_TOKEN>
}));
req.end();

 

 

 

 

This was cobbled together using what I understood from downloading and uploading, which I have successfully done using short-lived access tokens generated from the dashboard. Now however, I require access to the by dropbox app folder with a regular supply of access tokens.

1 Accepted Solution

Accepted Solutions

Здравко
Legendary | Level 20
Go to solution

Hi @AndyLei,

Do you understand that you mixed 'urlencoded' with 'JSON'? 😉

Hope this gives direction.

View solution in original post

3 Replies 3

Здравко
Legendary | Level 20
Go to solution

Hi @AndyLei,

Do you understand that you mixed 'urlencoded' with 'JSON'? 😉

Hope this gives direction.

Greg-DB
Dropbox Staff
Go to solution

I recommend using the official Dropbox JavaScript SDK if possible, as it will do much of the work for you. Alternatively, it may also just serve as a good example even if you can't use it.

 

Otherwise, refer to the documentation for /oauth2/token. It contains examples of calling the endpoint for different scenarios that you can translate to your platform.

 

For example, the error you posted here is indicating that the call failed because it's missing the required parameter "grant_type". I see you're attempting to set that, but you're trying to do so as JSON; this endpoints takes these parameters as application/x-www-form-urlencoded POST parameters, not JSON.

AndyLei
Explorer | Level 4
Go to solution

I appreciate pointing it out. I have changed to the following after some searching,  and now works as intended

 

 

import * as https from "https";

const req = https.request("https://api.dropbox.com/oauth2/token",{
    method: "POST",
    headers:{
        "Authorization":`Basic ${Buffer.from(`${<APP_KEY>}:${<SECRET_KEY>}`).toString("base64")}`,
        "Content-Type":"application/x-www-form-urlencoded"
    }
}, (res) => {
    res.on("data", function(rawdat){
        console.log(rawdat.toString());
    })
});
req.write(new URLSearchParams({
    grant_type:"refresh_token",
    refresh_token: <REFRESH_TOKEN>
}).toString());
req.end();

 

 

 

 

 

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    AndyLei Explorer | Level 4
  • User avatar
    Greg-DB Dropbox Staff
  • User avatar
    Здравко Legendary | Level 20
What do Dropbox user levels mean?