将文件从节点js传输到保管箱,无需基于浏览器的oauth身份验证 [英] Transfer files to dropbox from node js without browser based oauth authentication

查看:98
本文介绍了将文件从节点js传输到保管箱,无需基于浏览器的oauth身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个来自heroku的nodejs + express基于api服务器,并使用 dropbox-js 图书馆。以下是我想要做的:


  1. 一个用户点击一个特定的api端点并启动该过程。

  2. 通过节点进程生成一些文本文件并将其保存在服务器上

  3. 将这些文件传输到我拥有的使用我自己的凭据(用户和保管箱应用)的保管箱。

当随机用户需要这样做时,永远不会有这种情况..这是一个团队帐户,这是一个内部工具。



绊倒我的部分是,该保管箱想要打开一个浏览器窗口,并获得我的许可连接到该应用程序。问题是当我在heroku实例上运行的时候显然不能点击按钮。



是否有任何方式授权我们全面访问该应用在节点?



我觉得我可以使用phantomJS过程来点击按钮 - 但是似乎太复杂了,我想避免如果可能的话。 / p>

这是我的身份验证码:

  //库
var Dropbox = require('dropbox');

var DROPBOX_APP_KEY =key;
var DROPBOX_APP_SECRET =secret;

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

dbClient.authDriver(new Dropbox.Drivers.NodeServer(8191));

dbClient.authenticate(function(error,client){
if(error){
console.log(有些狗屎试图通过dropbox进行身份验证);
console.log(error);
return;
}


client.writeFile(test.txt,sometext stat){
if(error){
console.log(error);
return;
}

console.log(file saved! );
console.log(stat);
});
});


解决方案

让我进行一些测试,但这是可能的。



首先,您需要通过浏览器进行身份验证,并保存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
...
});

一旦你有了令牌和秘密,你可以在 Dropbox.Client 构造函数:

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

之后,您不必再需要通过浏览器进行身份验证(或至少除非有人运行代码,而不用令牌和秘密,这将使Dropbox生成一个新的令牌/秘密对,使旧的无效,或者应用程序凭据被撤销)。


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 and kicks off the process.
  2. Generate some text files via a node process and save them on the server
  3. Transfer these files to a dropbox that I own using my own credentials (user and dropbox app).

There will never be a case when a random user needs to do this.. it's a team account and this is an internal tool.

The part that is tripping me up is that dropbox wants to open a browser window and get permission from me to connect to the app. The issue is that I obviously can't click the button when the process is running on the heroku instance.

Is there any way for me to authorize access to the app totally in node?

I feel like I could potentially use a phantomJS process to click the button - but it seems too complicated and I'd like to avoid it if possible.

Here is my authentication code:

    // Libraries
    var Dropbox         = require('dropbox');

    var DROPBOX_APP_KEY    = "key";
    var DROPBOX_APP_SECRET = "secret";

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

    dbClient.authDriver(new Dropbox.Drivers.NodeServer(8191));

    dbClient.authenticate(function(error, client) {
      if (error) {
        console.log("Some shit happened trying to authenticate with dropbox");
        console.log(error);
        return;
      }


      client.writeFile("test.txt", "sometext", function (error, stat) {
        if (error) {
          console.log(error);
          return;
        }

        console.log("file saved!");
        console.log(stat);
      });
    });

解决方案

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).

这篇关于将文件从节点js传输到保管箱,无需基于浏览器的oauth身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆