Skip to content

Instantly share code, notes, and snippets.

@Laurian
Created April 22, 2015 15:54
Show Gist options
  • Save Laurian/a35422e9927bf859710c to your computer and use it in GitHub Desktop.
Save Laurian/a35422e9927bf859710c to your computer and use it in GitHub Desktop.
How to get the Dropbox access token server-side (express.js) with https://github.com/dropbox/dropbox-js, inspired by https://gist.github.com/davidmerfield/9335c6f7506091ad606e
app.get('/dropbox/auth', function(req, res) {
var client = new Dropbox.Client({ key: "*********", secret: "*********"});
client.authDriver({
authType: function() {return "code";},
url: function() {return "https://SERVER/dropbox/auth-callback";},
doAuthorize: function(authUrl, stateParam, client, callback) {
res.redirect(authUrl);// redirect to Dropbox
},
oauthQueryParams: ['access_token', 'expires_in', 'scope', 'token_type', 'code', 'error', 'error_description', 'error_uri', 'mac_key', 'mac_algorithm'].sort()
});
client.authenticate(function(error, client) {
if (error) return console.log(error);
});
});
app.get('/dropbox/auth-callback', function(req, res) {
var code = req.query.code;
var client = new Dropbox.Client({ key: "*********", secret: "*********"});
client.authDriver({
authType: function() {return "code";},
url: function() {return "https://SERVER/dropbox/auth-callback";},
doAuthorize: function(authUrl, stateParam, client, callback) {
return callback({code: code});// this is the code from the callback
},
oauthQueryParams: ['access_token', 'expires_in', 'scope', 'token_type', 'code', 'error', 'error_description', 'error_uri', 'mac_key', 'mac_algorithm'].sort()
});
// this time, this will use the code to get the oauth token
client.authenticate(function(error, client) {
if (error) return console.log(error);
console.log(client._oauth._token); // <-- TOKEN
// store the token in a db, use it whenever needed
// to get the user Dropbox id use client.getAccountInfo(function(error, accountInfo) {});
return res.redirect('/');
});
});
@ashton
Copy link

ashton commented Jun 3, 2015

Hey, saw your gist on an issue on dropbox-js repo..

Came here just to say thanks! I was trying to put this to work for years!

@djaax
Copy link

djaax commented Sep 14, 2015

That's really helpful, thanks a lot! Do you have an idea on how to use the token in upcoming (server-side) request? As the Documentation and a blog (https://blogs.dropbox.com/developers/2013/07/using-oauth-2-0-with-the-core-api/) says they expect a token Bearer to be send. My Idea was to store the token in a cookie and send it with every api call, but I cant't find a way using client.authenticate() or client.authDriver().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment