auth/google-oauth.js
function getToken(data, callback) {

    request.post({url: TOKEN_URL, form: data}, function (err, resp, body) {
        
        //this would be something like a connection error
        if (err) {
            return callback({statusCode: 500, message: err.message});
        }

        if (resp.statusCode !== 200) { //error
            return callback({statusCode: resp.statusCode, message: JSON.parse(body)});
        } else {
            debug('Got response back from token endpoint', body);
            /*
             Body should look like
             {
             "access_token": "ya29.UQH0mtJ5M-CNiXc-mDF9II_R7CRzRQm6AmxIc6TjtouR_HxUtg-I1icHJy36e065UUmIL5HIdfBijg",
             "token_type": "Bearer",
             "expires_in": 3600,
             "refresh_token": "1/7EBZ1cWgvFaPji8FNuAcMIGHYj3nHJkNDjb7CSaAPfM",
             "id_token": "..."
             }
             */
            var authData = JSON.parse(body);

            //id_token is a JWT token.  Since we're getting this directly from google, no need to authenticate it
            var idData = jwt.decode(authData.id_token);

            /*
             Sample id data
             { iss: 'accounts.google.com',
             sub: '103117966615020283234',
             azp: '107463652566-jo264sjf04n1uk17i2ijdqbs2tuu2rf0.apps.googleusercontent.com',
             email: 'foo@gmail.com',
             at_hash: 'qCF-HkLhiGtrHgv6EsPzQQ',
             email_verified: true,
             aud: '107463652566-jo264sjf04n1uk17i2ijdqbs2tuu2rf0.apps.googleusercontent.com',
             iat: 1428691188,
             exp: 1428694788 }
             */
            return callback(null, {
                id: idData.sub,
                email: idData.email,
                access_token: authData.access_token,
                expiration: Date.now() + (1000 * authData.expires_in),
                refresh_token: authData.refresh_token || data.refresh_token
            });
        }
    });
}
function getToken(data, callback) {

    request.post({url: TOKEN_URL, form: data}, function (err, resp, body) {
        
        //this would be something like a connection error
        if (err) {
            return callback({statusCode: 500, message: err.message});
        }

        if (resp.statusCode !== 200) { //error
            return callback({statusCode: resp.statusCode, message: JSON.parse(body)});
        } else {
            debug('Got response back from token endpoint', body);
            /*
             Body should look like
             {
             "access_token": "ya29.UQH0mtJ5M-CNiXc-mDF9II_R7CRzRQm6AmxIc6TjtouR_HxUtg-I1icHJy36e065UUmIL5HIdfBijg",
             "token_type": "Bearer",
             "expires_in": 3600,
             "refresh_token": "1/7EBZ1cWgvFaPji8FNuAcMIGHYj3nHJkNDjb7CSaAPfM",
             "id_token": "..."
             }
             */
            var authData = JSON.parse(body);

            //id_token is a JWT token.  Since we're getting this directly from google, no need to authenticate it
            var idData = jwt.decode(authData.id_token);

            /*
             Sample id data
             { iss: 'accounts.google.com',
             sub: '103117966615020283234',
             azp: '107463652566-jo264sjf04n1uk17i2ijdqbs2tuu2rf0.apps.googleusercontent.com',
             email: 'foo@gmail.com',
             at_hash: 'qCF-HkLhiGtrHgv6EsPzQQ',
             email_verified: true,
             aud: '107463652566-jo264sjf04n1uk17i2ijdqbs2tuu2rf0.apps.googleusercontent.com',
             iat: 1428691188,
             exp: 1428694788 }
             */
            return callback(null, {
                id: idData.sub,
                email: idData.email,
                access_token: authData.access_token,
                expiration: Date.now() + (1000 * authData.expires_in),
                refresh_token: authData.refresh_token || data.refresh_token
            });
        }
    });
}
function getToken ( data , callback ) { request . post ( { url : TOKEN_URL , form : data } , function ( err , resp , body ) { //this would be something like a connection error if ( err ) { return callback ( { statusCode : 500 , message : err . message } ) ; } if ( resp . statusCode !== 200 ) { //error return callback ( { statusCode : resp . statusCode , message : JSON . parse ( body ) } ) ; } else { debug ( 'Got response back from token endpoint' , body ) ; /*
             Body should look like
             {
             "access_token": "ya29.UQH0mtJ5M-CNiXc-mDF9II_R7CRzRQm6AmxIc6TjtouR_HxUtg-I1icHJy36e065UUmIL5HIdfBijg",
             "token_type": "Bearer",
             "expires_in": 3600,
             "refresh_token": "1/7EBZ1cWgvFaPji8FNuAcMIGHYj3nHJkNDjb7CSaAPfM",
             "id_token": "..."
             }
             */ var authData = JSON . parse ( body ) ; //id_token is a JWT token.  Since we're getting this directly from google, no need to authenticate it var idData = jwt . decode ( authData . id_token ) ; /*
             Sample id data
             { iss: 'accounts.google.com',
             sub: '103117966615020283234',
             azp: '107463652566-jo264sjf04n1uk17i2ijdqbs2tuu2rf0.apps.googleusercontent.com',
             email: 'foo@gmail.com',
             at_hash: 'qCF-HkLhiGtrHgv6EsPzQQ',
             email_verified: true,
             aud: '107463652566-jo264sjf04n1uk17i2ijdqbs2tuu2rf0.apps.googleusercontent.com',
             iat: 1428691188,
             exp: 1428694788 }
             */ return callback ( null , { id : idData . sub , email : idData . email , access_token : authData . access_token , expiration : Date . now ( ) + ( 1000 * authData . expires_in ) , refresh_token : authData . refresh_token || data . refresh_token } ) ; } } ) ; }
Makes a call to the google token service using the given data

Data should either have a grant type of refresh_token or authorization_code
Callback is of the form callback(err, result) where result contains some auth data, such as
id, email, access_token, expiration, and refresh_token
@param data
@param callback
Makes a call to the google token service using the given data