我正在从 heroku 运行基于 nodejs + express 的 api 服务器并使用 dropbox-js图书馆。这是我想做的:
- 用户点击特定的 api 端点并启动流程。
- 通过 Node 进程生成一些文本文件并保存在服务器上
- 使用我自己的凭据(用户和 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/