【问题标题】:access user profile via oauth tokens passport.js通过 oauth 令牌 passport.js 访问用户个人资料
【发布时间】:2015-05-03 19:31:26
【问题描述】:

我一直在使用passport.js。它工作正常,我可以轻松地从 API 获取 oauth_tokenVerifier

在 passport.js 中,每个 API 都有一个策略,基本上决定了如何与那个 API 服务器通信。最后回调以获取返回的用户配置文件。

但我没有看到自己使用oauth_token 获取profile 的方法。它在 Oauth 身份验证结束时一次性拍摄,以将数据保存在 session 或 db 中。

我有什么方法可以使用我的oauth_token 随时​​使用passport 方法直接访问用户个人资料。

在原型中,我看到了 userProfile 函数,它可以满足我的需要,但它以某种方式是私有方法。我不知道怎么用它

更新 1

我一直在为 passport.js 清理 git 存储库,我知道他们使用“node-oauth”来管理对 API 服务器的调用。这适用于任何策略_oauth

但我不知道要调用什么来获取资源令牌。此外,我必须逐步启动回调中的所有 API 调用,以模仿令牌访问调用。有什么标准方法可以做到这一点。

【问题讨论】:

    标签: javascript node.js oauth passport.js


    【解决方案1】:

    没有深入研究代码(但之前使用过 Passport),我假设 oauth_token 与用户数据一起存储在您的数据库中。您可能必须直接访问您的数据库模型才能获取令牌,然后您可以将其与提供程序 API 一起使用以访问您需要的信息。

    【讨论】:

      【解决方案2】:

      访问以下页面以了解如何使用 _oauth 属性。

      https://github.com/ciaranj/node-oauth

      它是这样工作的:

      oauth.get(
        'https://api.twitter.com/1.1/trends/place.json?id=23424977',
        'your user token for this app', //test user token
        'your user secret for this app', //test user secret            
        function (e, data, res){
          if (e) console.error(e);        
          console.log(require('util').inspect(data));
          done();      
        });   
      

      如果您实施自己的 OAuth1.0 策略,您可以轻松覆盖 以下方法来实现您自己的从远程 OAuth 服务器获取配置文件数据的逻辑:

      /**
       * @overwritten 
       * @method userProfile 
       * @class OAuth1Strategy
       * @description used by OAuth1Strategy class while authenticating 
       *  to get the user profile from the server 
       */
      OAuth1Strategy.prototype.userProfile = 
       function(token, tokenSecret, params, done) {
      
        this._oauth.get(
          config.profileUrl,
           token, 
          tokenSecret,         
          function (e, data, res){
            if (e) console.error(e);        
            console.log(require('util').inspect(data));
            done(e, data);      
          }); 
      };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-31
        • 2016-10-19
        • 2023-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多