【问题标题】:Dropbox api V2, get access token in query param instead of url hash (#) (Nodejs)Dropbox api V2,在查询参数中获取访问令牌而不是 url 哈希 (#) (Nodejs)
【发布时间】: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


    【解决方案1】:

    没错,片段(又名哈希)的内容对服务器不可见,只有客户端(浏览器)可见。 OAuth 2“令牌”流在片段上发送访问令牌,主要用于客户端应用程序,例如浏览器中的 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

    【讨论】:

    • 感谢您的精彩回答!我会关注这个帖子,我会尝试这里提供的解决方案 (npmjs.com/package/dropbox-client-oauth2) 或者我会直接使用 http api 而不是 javascript 的,效果很好。
    • SDK 现在支持代码流。更多信息here.
    • 我也收到了 GitHub 问题帖子的通知,但感谢您的更新!
    【解决方案2】:

    如果不想直接调用HTTP,可以使用我的小dropbox-v2-apiwrapper包:

    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());
    });
    

    【讨论】:

    • 谢谢,我会看看你的图书馆:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-09
    • 2012-05-27
    • 2014-02-08
    • 2019-10-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多