Search code examples
node.jspassport.jsgoogle-contacts-api

How to get Google contacts without access token?


UPDATED

I got my answer still with the help of access token :)

And going to mention it.

I am using Google contacts API to get user contacts.

I am using passport.js to login with Google and with help of passport's access token I am calling the API

https://www.google.com/m8/feeds/contacts/default/full?max-results=999999&alt=json&oauth_token=' + token

and getting the all the contacts

But I need to use something else instead of access token like secret key or client key.

Because every time I need to log in with Google for syncing the contacts if user added newly contact.

I did Google but didn't get any solution.

any idea will be helpful for me.

Here is my code to get Contacts

  var getGoogleContacts = function(token, userId) {
        var url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results=999999&alt=json&oauth_token=' + token;
        request(url, function(error, response, body) {
            if (error) {
                console.log(error);
            } else {
                var contacts = JSON.parse(body);
                saveGoogleContacts(userId, contacts);
            }
        });

    };

    /*Get contacts and store in user_contact table*/
    var saveGoogleContacts = function(userId, contacts) {
        var gContacts = [];
        contacts.feed.entry.forEach(function(contact, index) {
            if (contacts.feed.entry[index].gd$email) {
                gContacts.push([
                    null, null, contacts.feed.entry[index].title.$t, "'" + contacts.feed.entry[index].gd$email[0].address + "'",
                    1, userId, 0
                ]);
            }
        });
        if (gContacts.length > 0) {
            user.insertContacts(gContacts, function(err, result) {
                if (err) {
                    console.log(err);
                } else {
                    console.log('contacts saved: ' + result);
                }
            });
        }else{
            console.log('No records available');
        }
    }; 

Solution

  • Here I got my answer.

    As I mentioned I am using passport.js to log in with Google

    And during log in process passport provides access token and refresh token by default refresh token will be null.

    If you want to get refresh token you need to pass parameter accessType:offline during authentication process, Like this

    app.get('/auth/google', passport.authenticate('google', { scope : ['profile', 'email','https://www.google.com/m8/feeds'],accessType: 'offline', approvalPrompt: 'force' }));

    After that you'll get refresh token and store it in any permanent place since it will not be expire and can use whenever you want to get access token

    To get access token I am using refresh-token module.

    After gertting token just need to call the API to get contacts.

    Like this

    var url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results=999999&alt=json&oauth_token=' + token;
            request(url, function(error, response, body) {
                if (error) {
                    cb(error);
                } else {
                    var contacts = JSON.parse(body);
                    cb(null, contacts);
                }
            });
        }
    

    That's it.