We are aware of the issue with the badge emails resending to everyone, we apologise for the inconvenience - learn more here.

Forum Discussion

AndyLei's avatar
AndyLei
Explorer | Level 4
3 years ago

Help with generating access tokens in Node.js

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.

    • AndyLei's avatar
      AndyLei
      Explorer | Level 4

      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();

       

       

       

       

       

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

    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.