Created
April 22, 2015 15:54
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('/'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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().