How to get account information of Dropbox user using nodejs
Posted By : Chandan Kumar Singh | 18-May-2016
You need a Dropbox API key to make API requests
a. Go to: https://www.dropbox.com/developers/apps
b. If you've already registered an app, click on the app link to see the app's API key and secret ?
c. Otherwise, click "Create an app" to register an app. Choose "Dropbox API app",
then “Choose the type of access you need” depending on your needs. Then name of your app.
d. Enter redirect url (localhost:3000/api/v1/dbox)
I am keeping all the function in a file called Dropbox.js
var request = require('request');
exports.Authenticate = function(ckey, csecret, redirect_uri, cb) {
var err = "";
var redirect_url = "";
if(ckey === "" || csecret === "" || redirect_uri === "") {
err = "Missing client key and/or client secret key.";
}else{
redirect_url = “https://www.dropbox.com/oauth2/authorize?client_id=" + ckey + "&response_type=code&redirect_uri=" + redirect_uri;
}
//to get the access token
exports.AccessToken = function(ckey, csecret, auth_code, redirect_url, cb) {
var url = 'https://api.dropbox.com/1/oauth2/token';
var body = {
"code": auth_code,
"grant_type": "authorization_code",
"redirect_uri": redirect_url,
"client_id": ckey,
"client_secret": csecret
};
request.post(url, {form: body, json: true}, function(err, res, body) {
cb(err, body);
});
};
cb(err, redirect_url);
};
exports.api = function(access_token) {
return {
account: function(cb) {
options = {
url: 'https://api.dropbox.com/1/account/info',
headers: {
"Authorization": "Bearer " + access_token,
},
json: true
};
request.get(options, function(err, res, body) {
cb(err, res, body);
});
},
};
}
Now make file called drop.js
var node_dropbox = require('./Dropbox.js');
var key = 'ikeap8bxxxxxxxx';
var secret= 'xxxxxxxxxxxxxxx';
var redirect_url = 'http://localhost:3000/api/v1/dbox';
node_dropbox.Authenticate(key, secret, redirect_url, function(err, url){
console.log(url);//this is the url where user will be redirected
// looks like this: "https://www.dropbox.com/1/oauth2/authorize?client_id=ikeap8bxxxxxxxx&response_type=code&redirect_uri=http://localhost:3000/api/v1/dbox"
});
module.exports.dbox = (function () {
var getToken = function(code) {
node_dropbox.AccessToken(key, secret, code, redirect_url, function(err, body) {
Var access_token = body.access_token;
api = node_dropbox.api(access_token);
api.account(function(err, res, body) {
console.log(body);
});
}
};
return {
getToken:getToken
};
})();
Now run drop.js
node drop.js
Get the url and and hit in the browser, get the param code by req.query.code and call the method get token.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Chandan Kumar Singh
Chandan is a bright Web Developer with expertise in Java and Spring framework and ORM tools Hibernate. He loves technologies like Blockchain and IoT.