Transfer files to dropbox from node js without browser based oauth authentication

后端 未结 2 498
春和景丽
春和景丽 2021-02-02 14:31

I am running a nodejs + express based api server from heroku and using the dropbox-js library. Here\'s what I\'d like to do:

  1. A user hits a specific api endpoint a
相关标签:
2条回答
  • 2021-02-02 14:44

    Took me a bit of testing, but it's possible.

    First, you need to authenticate through the browser and save the token and token secret that are returned by Dropbox:

    dbClient.authenticate(function(error, client) {
      console.log('connected...');
      console.log('token ', client.oauth.token);       // THE_TOKEN
      console.log('secret', client.oauth.tokenSecret); // THE_TOKEN_SECRET
      ...
    });
    

    Once you have the token and the secret, you can use them in the Dropbox.Client constructor:

    var dbClient = new Dropbox.Client({
      key         : DROPBOX_APP_KEY,
      secret      : DROPBOX_APP_SECRET,
      sandbox     : false,
      token       : THE_TOKEN,
      tokenSecret : THE_TOKEN_SECRET
    });
    

    After that, you won't get bothered with having to authenticate through a browser anymore (or at least not until someone runs the code again without the token and the secret, which will make Dropbox generate a new token/secret pair and invalidate the old ones, or the apps credentials are revoked).

    0 讨论(0)
  • 2021-02-02 14:54

    Or you can just use the Implicit grant and get the oauth token.

            var client = new Dropbox.Client({
                key: "xxxxx",
                secret: "xxxxx",
                token:"asssdsadadsadasdasdasdasdaddadadadsdsa", //got from implicit grant
                sandbox:false
            });
    

    No need to get to the browser at all.This line is no longer required!

       client.authDriver(new Dropbox.AuthDriver.NodeServer(8191));
    
    0 讨论(0)
提交回复
热议问题