javascript - Dropbox API - Node.js - 记住身份验证

标签 javascript node.js dropbox-api

我正在使用 Node.js 上传和下载一些文件。

身份验证部分有效,但每次启动“应用程序”时,您都必须重新验证该应用程序。

代码:

<html>
<title>Dropbox</title>

<button id="signin-button">Sign in</button>
<script>
require("nw.gui").Window.get().maximize(true);


var Dropbox = require("dropbox");

// Server-side applications use both the API key and secret.
var client = new Dropbox.Client({
    key: "XXXXXXXXXXX",
    secret: "XXXXXXXXXX"
});

   client.authDriver(new Dropbox.AuthDriver.NodeServer(8191));


// Try to use cached credentials.
client.authenticate({interactive: false}, function(error, client) {
  if (error) {
return showError(error);
 }
  if (client.isAuthenticated()) {
    // Cached credentials are available, make Dropbox API calls.
    console.log("Authenticated form Cache");
  } else {
// show and set up the "Sign into Dropbox" button
var button = document.querySelector("#signin-button");
button.setAttribute("class", "visible");
button.addEventListener("click", function() {
  // The user will have to click an 'Authorize' button.
  client.authenticate(function(error, client) {
    if (error) {
      return showError(error);
    }
    console.log("Authenticated after button click.");
  });
    });
  }
});


function showError(error){
console.error(error);
}
</script>
</html>

身份验证示例代码位于他们的 github 上。它从不加载或保存身份验证数据。

tl;dr 如何保存身份验证数据以防止用户每次都必须登录。

杰罗恩

最佳答案

您可以在身份验证后通过 client.credentials().token 提取访问 token 并保留它。

然后在后续运行中,您可以使用 new Dropbox.Client({ token: savingToken }); 以避免再次通过 OAuth。

关于javascript - Dropbox API - Node.js - 记住身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27133278/

相关文章:

Swift Dropbox 进度条

dropbox-api - Dropbox Datastore - 跨用户帐户共享数据

python - 使用 Python Dropbox API 的 UnboundLocalError 问题

javascript - Hapi-Joi 验证有效负载可以是对象数组还是简单的 Javascript 对象?

javascript - 使用数组对对象进行排序并将值存储在另一个数组中

node.js - Mongoose 将 _id 更改为 id

java - Single Sign on Java and NodeJs (Share single session object between Java and Nodejs)

javascript - html5 视频标签无法使用 cordova 2.9.0 和 jquery1.9.0 在 phonegap android 中播放 .mp4 视频?

javascript - 如何检测浏览器的缓存设置(开/关)

node.js - Heroku 上的生产构建中显示开发构建错误