我在我的Nodejs应用上使用official Dropbox API (V2)。 这听起来像一个愚蠢的问题,但我真的无法找到如何从回调网址获取给定的访问令牌。实际上,它应该在 url的哈希(#)部分(根据他们的文档和javascript client-side exemple),这是服务器端看不到的......
我找不到任何来自nodejs app的身份验证例子,只使用基本的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");
}
}
感谢您的帮助!
答案 0 :(得分:2)
这是正确的,片段a.k.a.哈希的内容对服务器是不可见的,只有客户端(浏览器)可见。 OAuth 2"令牌" flow在片段上发送访问令牌,主要用于客户端应用程序,例如浏览器中的JavaScript。 OAuth 2"代码"而是为服务器端应用程序发送授权代码作为URL参数。
如果您有兴趣,可以在Dropbox /oauth2/authorize documentation中找到有关这两种不同流量的更多信息。
Dropbox API v2 JavaScript SDK目前仅支持"令牌"流,但we're tracking this as a feature request for support for the "code" flow。
答案 1 :(得分:1)
如果您不想直接调用HTTP,可以使用我的小dropbox-v2-api包装器包:
const dropboxV2Api = require(dropbox-v2-api');
const dropbox = dropboxV2Api.authenticate({
client_id: 'APP_KEY',
client_secret: 'APP_SECRET',
redirect_uri: 'REDIRECT_URI'
});
//generate and visit authorization sevice
const authUrl = dropbox.generateAuthUrl();
//after redirection, you should receive code
dropbox.getToken(code, (err, response) => {
//you are authorized now!
});
完整示例(see here):
const dropboxV2Api = require(dropbox-v2-api');
const Hapi = require('hapi');
const fs = require('fs');
const path = require('path');
const Opn = require('opn');
const credentials = JSON.parse(fs.readFileSync(path.join(__dirname, 'credentials.json')));
//set auth credentials
const dropbox = dropboxV2Api.authenticate({
client_id: credentials.APP_KEY,
client_secret: credentials.APP_SECRET,
redirect_uri: 'http://localhost:5000/oauth'
});
//prepare server & oauth2 response callback
const server = new Hapi.Server();
server.connection({ port: 5000 });
server.route({
method: 'GET',
path: '/oauth',
handler: function (request, reply) {
var params = request.query;
dropbox.getToken(params.code, function(err, response){
console.log('user\'s access_token: ',response.access_token);
//call api
dropbox({
resource: 'users/get_current_account'
}, function(err, response){
reply({response: response});
});
});
}
});
server.start(function(){
//open authorization url
Opn(dropbox.generateAuthUrl());
});