3

我目前正在考虑使用 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 覆盖?

4

1 回答 1

4

我不熟悉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。这样,您就可以使用熟悉的模块从任何提供者访问任何API 服务。

于 2013-07-29T20:23:52.953 回答