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

后端 未结 2 497
春和景丽
春和景丽 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).

提交回复
热议问题