【问题标题】:Using PassportJS google authentication with the nodejs google api library将 PassportJS google 身份验证与 nodejs google api 库一起使用
【发布时间】:2013-07-29 05:11:36
【问题描述】:

我目前正在考虑使用 nodejs 客户端实现一个 google api:

https://github.com/google/google-api-nodejs-client/

我正在尝试使用护照进行身份验证,这似乎可以正常工作@

passport.use(new GoogleStrategy({
  clientID: GOOGLE_CLIENT_ID,
  clientSecret: GOOGLE_CLIENT_SECRET,
  callbackURL: "http://localhost:3000/auth/google/callback"
},
function(accessToken, refreshToken, profile, done) {
    process.nextTick(function () {

      var user = {
        id: profile.id,
        email: profile.email,
        firstName: profile.given_name,
        lastName: profile.family_name,
        accessToken: accessToken
      };

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

在我的谷歌身份验证回调中:

app.get('/auth/google/callback',
  passport.authenticate('google', { failureRedirect: '/login' }),
  function(req, res) {

    //do something here with the google api

  });

我可以得到req.user,这里有accessToken

但是,Google api nodejs 客户端的文档并不清楚如何使用 accessToken。 包含的示例显示 OAauth2Client 检索令牌,但我猜这部分已经使用 Passport 覆盖?

【问题讨论】:

  • 是的,一旦 Passport 调用成功回调,它就成功了,并且有profile,所以我假设它已经完成了所有比较令牌或其他的工作。

标签: node.js google-api passport.js google-api-client


【解决方案1】:

我不熟悉 google-api-nodejs-client,但这在他们的文档中:

oauth2Client.credentials = {
  access_token: 'ACCESS TOKEN HERE',
  refresh_token: 'REFRESH TOKEN HERE'
};

client
  .plus.people.get({ userId: 'me' })
  .withAuthClient(oauth2Client)
  .execute(callback);

我假设您可以将凭据设置为 Passport 提供的凭据,一切都会正常工作。

不过,老实说,我发现这些 API 包装器真的很做作,建议只使用 request。这样您就可以使用熟悉的模块从 any 提供商访问 any API 服务。

【讨论】:

  • 我确认,这行得通import google from 'googleapis'; const plus = google.plus('v1'); const oauth2Client = new google.auth.oauth2Client;
猜你喜欢
  • 2016-03-01
  • 1970-01-01
  • 2012-04-23
  • 1970-01-01
  • 2017-09-22
  • 2015-10-21
  • 2019-01-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多