Search code examples
node.jsdropboxdropbox-sdk-js

How to authenticate with dropbox using dropbox-js on arbitrary cloud platform?


I have successfully implemented dropbox login on local machine using dropbox-js.

The authentication is performed with the following function:

router.get('/dropbox', isAuthenticated, function(req, res, next) {
    var isLoggedIn = !!req.user;
    var dropboxClient = req.app.get('dropboxClient');


    dropboxClient.authenticate(function(error, client) {
        if (error) {

            console.log(error);
        }
        dropboxClient.readdir("/", function(error, entries) {
            if (error) {
                console.log(error);  // Something went wrong.
                return 0;
            }
            console.log(entries);
            var folders = getFolders(entries);
            console.log("Your Dropbox contains " + folders.join(", "));
            res.render('settings', { title: 'Express', loggedIn: isLoggedIn, folders: folders });
        });
    });
});

the dropboxClient is defined in app.js as follows:

var dropboxClient = new Dropbox.Client({
  key: dropboxConfig.dropbox.app_key,
  secret: dropboxConfig.dropbox.app_secret
});
dropboxClient.authDriver(new Dropbox.AuthDriver.NodeServer(8191));

and the OAuth 2 redirect URI defined in dropbox app console is the following:

http://localhost:8912/oauth_callback

As mentioned, when the app is running locally, everything works fine, but when I transfer it on cloud platform I get the response 502 (Bad gateway).

I tried to add the OAuth2 redirect URI myapp.evennode.com:8912/oauth_callback

but it is not working. Has anyone managed to successfully implement dropbox authentication on any cloud platform (heroku, evennode...) using dropbox-js?

I would appreciate any help.


Solution

  • Dropbox.AuthDriver.NodeServer is for testing purposes... it listens on localhost and opens a browser on the local machine (which certainly won't work when deployed elsewhere).

    If you're trying to auth in the browser, see https://blogs.dropbox.com/developers/2013/12/writing-a-file-with-the-dropbox-javascript-sdk/ for a basic example using dropbox.js.

    If you need to use the server-side flow in node.js, dropbox.js won't be of much help. See https://github.com/smarx/othw/tree/master/Node.js for a simple example.