3

I'm currently looking at implementing a google api, using the nodejs client:

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

I'm trying to use passport in order to authenticate, which seems to be working@

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

In my google auth callback:

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

    //do something here with the google api

  });

I can get hold of req.user, and this has the accessToken

However, the docs for the Google api nodejs client aren't clear on how to use an accessToken. The example included shows the OAauth2Client retrieving a token, but I guess that part has already been covered using Passport?

1
  • Yes, once Passport invokes the success callback, it's succeeded and has profile, so I assume that it has already done all the work of comparing tokens or whatever. Commented Jul 29, 2013 at 15:25

1 Answer 1

5

I'm not familiar with google-api-nodejs-client, but this is in their documentation:

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

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

I assume you can just set the credentials to those provided by Passport, and things will work fine.

Honestly, though, I find these API wrappers really contrived, and recommend just using request. That way you can access any API service from any provider using a familiar module.

Sign up to request clarification or add additional context in comments.

1 Comment

I confirm, this works just do import google from 'googleapis'; const plus = google.plus('v1'); const oauth2Client = new google.auth.oauth2Client;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.