Obtain the user profile of token and gmail refreshed in node.js using OAuth2

advertisements

I would like to authorize the gmail login in my app using simple-oauth2 in node.js and also trying to get the user profile ,initially Im doing the authentication ,the code is

    var oauth2 = require('simple-oauth2')({
    clientID: configAuth.googleAuth.clientID,
    clientSecret: configAuth.googleAuth.clientSecret,
    site: 'https://accounts.google.com/o/',
    tokenPath: 'https://accounts.google.com/o/oauth2/token',
    authorizationPath: 'oauth2/auth'
});

// Authorization uri definition

var authorization_uri = oauth2.authCode.authorizeURL({
    redirect_uri: 'http://localhost:8080/auth/google/callback',
    scope: 'https://www.googleapis.com/auth/userinfo.profile',
    scope: 'https://www.googleapis.com/auth/analytics.readonly',
    scope: 'https://www.googleapis.com/auth/userinfo.email',
    state: '382332$'
});
app.get('/auth/google', function (req, res) {
    res.redirect(authorization_uri);
});

Callback is,

    app.get('/auth/google/callback', function (req, res) {
    var code = req.query.code;
     oauth2.authCode.getToken({
        code: code,
        redirect_uri: 'http://localhost:8080/auth/google/callback'
    }, saveToken);

    function saveToken(error, result, profile1) {
        if (error) {
            console.log('Access Token Error', error);
        }
        token = oauth2.accessToken.create(result);
        console.log('token',token)
    }

The result of token is like this :

{ access_token: 'ya29.lQKN8a2ECM0h5ECWL4NcThS924m_eMUWg9I3BVfRvkTiK-lt-     8_1bCLjBmtmi68Kg8k',
  token_type: 'Bearer',
  expires_in: 3599,
  id_token: 'somelongstring',
  expires_at: Sat Feb 27 2016 16:38:05 GMT+0530 (India Standard Time)
  }

In the token response I couldn't get the refresh token . Is there any other method to get refresh token ?

And also how to get the logged in user's profile detail once the login is success.Please suggest some solution ..! Thanks in advance !!


I have no experience with simple-oauth2 but I know with passport-google (http://passportjs.org/docs/google) it does what you require out-of-the-box.