【发布时间】:2018-01-22 10:46:30
【问题描述】:
我在我的 Nodejs 应用程序上使用 official Dropbox API (V2)。 这听起来像是一个愚蠢的问题,但我真的无法找出如何从回调 url 获取给定的访问令牌。实际上,它应该在 hash (#) 部分的 url 中(根据他们的文档和javascript client-side exemple),服务器端不可见...
我找不到任何从 nodejs 应用程序进行身份验证的示例,仅使用基本 api。
这是我的验证码:
我的快递应用:
//Entry point, DC is a DropboxConnector object
app.get('/connect/Dropbox', function(req, res) {
console.log('/connect/Dropbox called');
res.redirect(DC.getConnexionURL());
});
// Callback from the authentication
app.get('/authDropbox', function(req, res) {
console.log("/authDropbox called");
console.log(url.format(req.protocol + '://' + req.get('host') + req.originalUrl));
// The above log is: 'http://localhost:8080/authDropbox'
// Here is the problem, the access token is unreachable by express
DC.getToken(req.query.code, res);
connectorList.push(DC);
});
DropboxConnector.js,我的 Dropbox api 包装器:
var REDIRECT_URI = 'http://localhost:8080/authDropbox';
//The authentication url given by the dropbox api
getConnexionURL() {
dbx = new Dropbox({ clientId: CLIENT_ID});
var authUrl = dbx.getAuthenticationUrl(REDIRECT_URI);
console.log("AuthURL: " + authUrl);
return authUrl;
}
// @param code is supposed to be the access token...
getToken(code, res) {
if (!!code) {
dbx = new Dropbox({ accessToken: code });
console.log("Authenticated!");
res.redirect(CALLBACK_URL);
} else {
console.log("No code here");
}
}
感谢您的帮助!
【问题讨论】:
标签: node.js express oauth-2.0 dropbox-api