node.js - 使用返回的token访问github api

标签 node.js express passport.js github-api

我使用passportjs和passport-github在我的应用程序中创建社交登录,

passport.use(new GithubStrategy(
  {
    clientID     : configAuth.githubAuth.clientID,
    clientSecret : configAuth.githubAuth.clientSecret,
    callbackURL  : configAuth.githubAuth.callbackURL
  },
  function(token, refreshToken, profile, done) {
    process.nextTick(function() {
      User.findOne({'github.id' : profile.id}, function(err, user) {
        if (err) {
          return done(err);
        }
        if (user) {
          return done(null, user);
        } else {
          var newUser = new User();
          newUser.github.id = profile.id,
          newUser.token     = token,
          newUser.name      = profile.displayName;
          newUser.email     = profile.emails[0].value;
          newUser.username  = profile.username;
          // save
          newUser.save(function(err){
            if (err) {
              throw err;
            }

            return done(null, newUser);
          });
        }
      });
    });
  }
));

现在我正在使用另一个名为 octonode 的组件,它需要一个 access_token 来验证其用户,回调中的 token 是否与此 access_token 相同,因为我在执行此操作时似乎没有经过身份验证:

var github = require('octonode');

exports.read = function (req, res, next) {

  var client = github.client();
  client.get('/user?access_token=' + req.user.token, {}, function (err, status, body, headers) {
    res.json(body);
  });
};

也尝试这样做:

var client = github.client(req.user.token);
client.get('/user',{}, function...)

我看到黑屏,这意味着没有响应。

最佳答案

好吧,正如 SO 中的一个答案所述:

Note that Passport does not actively use the access token or refresh token, other than to fetch the user profile during login. You're application is responsible for using these tokens when making whatever API requests are necessary. As such, you can implement either method you describe, Passport is not involved in the process.

access_tokens 会返回给您,但之后它不会处理它,您负责保存它或做任何您想做的事情。

我的代码基本上是受到 Scotch.io 使用 Passport 进行 facebook 身份验证的教程的启发。他们不会在每次登录时更新 token ,因为他们在教程中不需要,但他们确实将其保存在数据库中,检查他们的 source code

通过一些评论和调试,我发现这是我的应用程序中的罪魁祸首,因此我需要更新说明是否找到用户的条件,更新 token 和一些值,以便一些重要信息在登录时保留。

if (user) {
   user.token = token;
   user.name  = profile.displayName;
   user.email = profile.emails[0].value;
   user.save();
   return done(null, user);
}

现在就可以了:

var client = github.client(req.user.token);
client.get('/user', {}, function (err, status, body, headers) {
    res.json(body);
});

感谢@MikeSmithDev 帮助我。

关于node.js - 使用返回的token访问github api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25654617/

相关文章:

node.js - 使用 Passport 持久 session 时的 authToken 安全吗?

javascript - Node.js:取决于用户身份验证的条件内容

node.js - sockjs 140秒后关闭

javascript - 自动提示最合适的数据结构是什么?

node.js - NodeJS/Express - 在路由器文件中提供可用的 MySQL 连接对象

node.js - Express-Socket.IO 应用程序无法与我的 Azure WebApp 配合使用

node.js - 使用 express-session 和 passportjs 未从存储中正确获取 session

javascript - 使用回调函数进行快速路由时未定义参数

javascript - TypeError : $ . 查找不是函数

javascript - 在 express/react SSR 应用程序中使用 materialize-css