Node.js Dropbox api V2,在查询参数中获取访问令牌,而不是url哈希(#)(Nodejs)
我正在使用我的Nodejs应用程序上的。 这听起来像一个愚蠢的问题,但我真的不知道如何从回调url获取给定的访问令牌。实际上,它应该位于url的哈希(#)部分(根据他们的文档和文档),服务器端看不到它 我无法从nodejs应用程序中找到任何验证示例,仅使用基本api 这是我的身份验证码: 我的express应用程序:Node.js Dropbox api V2,在查询参数中获取访问令牌,而不是url哈希(#)(Nodejs),node.js,express,oauth-2.0,dropbox-api,Node.js,Express,Oauth 2.0,Dropbox Api,我正在使用我的Nodejs应用程序上的。 这听起来像一个愚蠢的问题,但我真的不知道如何从回调url获取给定的访问令牌。实际上,它应该位于url的哈希(#)部分(根据他们的文档和文档),服务器端看不到它 我无法从nodejs应用程序中找到任何验证示例,仅使用基本api 这是我的身份验证码: 我的express应用程序: //Entry point, DC is a DropboxConnector object app.get('/connect/Dropbox', function(req, r
//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");
}
}
谢谢你的帮助 没错,片段a.k.a.散列的内容对服务器不可见,只有客户端(浏览器)可见。OAuth 2“token”流在片段上发送访问令牌,主要用于客户端应用程序,例如浏览器中的JavaScript。OAuth 2“代码”流将授权代码作为URL参数发送给服务器端应用程序 如果您感兴趣,可以在中找到有关这两种不同流的更多信息
不幸的是,Dropbox API v2 JavaScript SDK目前只支持“令牌”流,但是 如果不想直接调用HTTP,可以使用我的微型包装包:
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!
});
完整示例():
谢谢你的回答!我将关注这个线程,我将尝试这里提供的解决方案(),或者我将直接使用http api而不是javascript api,它工作得非常好。SDK现在支持代码流。更多信息。我也收到了GitHub问题帖子的通知,但感谢您的更新!谢谢,我会看看你的图书馆:)
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());
});