Cut the Clutter: Test Ignore Files Feature - sign up to become a beta tester here.
Forum Discussion
AndyLei
3 years agoExplorer | Level 4
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.
3 Replies
- Здравко3 years agoLegendary | Level 20
- Greg-DB3 years ago
Dropbox Community Moderator
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.
- AndyLei3 years agoExplorer | 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();
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.6,039 PostsLatest Activity: 3 hours ago
The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.
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!