Hey! I am getting the same error when I try to get token through "https://api.dropboxapi.com/oauth2/token". It works in postman but when I try to access this route with header and request body with my node js website, it throws that error. I am not using Dropbox SDK for node. I am trying to replicate/learn from "nodegallerytutorial" posted on git, and in there I found this coode:
//Exchange code for token
if(req.query.code ){
let options={
url: config.DBX_API_DOMAIN + config.DBX_TOKEN_PATH,
//build query string
qs: {'code': req.query.code,
'grant_type': 'authorization_code',
'client_id': config.DBX_APP_KEY,
'client_secret':config.DBX_APP_SECRET,
'redirect_uri':config.OAUTH_REDIRECT_URL},
method: 'POST',
json: true
}
try{
let response = await rp(options);
//we will replace later cache with a proper storage
//mycache.set("aTempTokenKey", response.access_token, 3600);
await regenerateSessionAsync(req);
req.session.token = response.access_token;
res.redirect("/");
}catch(error){
return next(new Error('error getting token. '+error.message));
}
}
}
promise-request is deprecated now, so I tired using node-fetch and axios. Here is code that I am using for the same path (the callback path I mentioned in my dropbox console).
// here the version of the code that I used with axios
app.get("/callback", async (req,res)=>{
try {
const body = {
grant_type: "authorization_code",
code: req.query.code,
redirect_uri: "http://localhost:3000/callback",
client_id: client_id,
client_secret: client_id,
json:true
};
const response = axios.post("https://api.dropboxapi.com/oauth2/token", body);
console.log({response});
res.send({response});
} catch(err)
{
res.send({error: err});
}
});
// below is the code for node-fetch that I used
app.get("/callback", (req,res)=>{
const body = {
grant_type: "authorization_code",
code: req.query.code,
redirect_uri: "http://localhost:3000/callback",
client_id: client_id,
client_secret: client_secret
};
node_fetch("https://api.dropboxapi.com/oauth2/token", {
method:'POST',
body
}).then((response)=> response.json()).then((jsonObj)=>{
res.send({response:jsonObj});
}).catch((err)=>{
res.send({err});
});
});
Here is the error. Please note that everything works fine when I use psotman.
