node.js - 将文件从 Node js 传输到 Dropbox,无需基于浏览器的 oauth 身份验证

标签 node.js express dropbox-api

我正在从 heroku 运行基于 nodejs + express 的 api 服务器并使用 dropbox-js图书馆。这是我想做的:

  1. 用户点击特定的 api 端点并启动流程。
  2. 通过 Node 进程生成一些文本文件并保存在服务器上
  3. 使用我自己的凭据(用户和 Dropbox 应用)将这些文件传输到我拥有的 Dropbox。

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

让我感到困惑的部分是保管箱想要打开一个浏览器窗口并获得我的许可以连接到该应用程序。问题是当进程在 heroku 实例上运行时,我显然无法单击按钮。

我有什么方法可以完全在 Node 中授权访问应用程序?

我觉得我可能会使用 phantomJS 进程来单击按钮 - 但它似乎太复杂了,如果可能的话我想避免它。

这是我的验证码:

    // 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);
      });
    });

最佳答案

我做了一些测试,但这是可能的。

首先,您需要通过浏览器进行身份验证,并保存 Dropbox 返回的 token 和 token secret :

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

获得 token 和 key 后,您可以在 Dropbox.Client 构造函数中使用它们:

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

之后,您就不必再为必须通过浏览器进行身份验证而烦恼了(或者至少在有人在没有 token 和 key 的情况下再次运行代码之前不会,这将使 Dropbox 生成一个新的 token / key 对并且使旧的无效,或应用程序凭据被吊销)。

关于node.js - 将文件从 Node js 传输到 Dropbox,无需基于浏览器的 oauth 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16323806/

相关文章:

javascript - Javascript/Node 中从不执行用户代码 : is it possible, 的隐藏线程,如果是这样,是否会导致竞争条件的神秘可能性?

java - 使用 Dropbox Java API 将文件上传到 Dropbox

java - Dropbox API ListFolder 限制 2000 个条目

javascript - Express.js 中的“router.get”与 'router.route.get'

mysql - 错误: secretOrPrivateKey must have a value

node.js - 在 nodejs 中连接到 heroku postgres db 时出错

java - 如何使用 Dropbox API V2 下载特定文件?

javascript - 无法使用 JDBC (Node.js) 访问 ignite 数据库的元数据?

c++ - C++ 中的 NAPI native 模块正在部分执行(包括 std::thread)

javascript - 如何修复 angular 6 universal server.js 中的 "ReferenceError: window is not defined"错误