Jack Jack - 1 month ago 13
Node.js Question

Unable to sync Gmail contacts with passport-google-oauth in Node.js

I an new for passport.js.
I am using it to authenticate and getting

Gmail contacts
,for getting the contacts I need to pass
scope
value this
https://www.google.com/m8/feeds
.But I didn't get the contact list except profile details.

Here is my code stuff:

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

// the callback after google has authenticated the user
app.get('/auth/google/callback', passport.authenticate('google', {
successRedirect : '/user/dashboard',
failureRedirect : '/register.html'
}));


And my
passport.js
code:

passport.use(new GoogleStrategy1({
consumerKey: config.auth.googleAuth.clientID,
consumerSecret: config.auth.googleAuth.clientSecret,
callbackURL: config.auth.googleAuth.callbackURL
},
function(token, tokenSecret, profile, done) {
console.log(profile);
return done(err, user);
}
));


When I print
profile
I am getting only user details not
contact list
.
I don't have idea, what I do for getting that.
Any help will be appreciated.

Thank You.

Answer

These are the following steps are used to get the Gmail contacts

1- To communicate with any Google API we need to create an account on Google console

2- After that create a project, which we want to communicate with Google API, after creating project Google provides secret key and client key which is used to communicate with Google. These keys are required whenever our application try to communicate with any Google API.

3- To get the Gmail contacts, Google provides the https://www.google.com/m8/feeds/contacts/default/full?alt=json&oauth_token=xxxxxx

4- We only need to call this API for getting the contacts, and this API takes some credential before communicating.

5- The users must have logged in with Google and have the token which used by API to get the user’s contacts.

6- Normally we prefer passport Google strategy to log in with Google. And our half of the things are done by Passport.js like authentication with Google and token.

7- When user login with Google, the Passport.js plays a role as a middleware for the successful login and during that, passport provides a token of current user. And that time we are calling the contacts API https://www.google.com/m8/feeds/contacts/default/full?alt=json&oauth_token=xxxxxx

And easily we can get the token, and the token created by Google expires after one hour, but we should not worry about that since passport internally provides new token provided by Google.

Hope it will work for you.


Update

Let's play with REST API

Get all the contacts by using REST call

Use request module to request the HTTP call

request.get({
    url: 'https://www.google.com/m8/feeds/contacts/default/full',
    headers: {
        'Authorization': 'Bearer <Your access token>',
        'Content-Type': 'application/json'
    },
    qs: qs,//Optional to get limit, max results etc
    method: 'GET'
    }, function (err, response, body) {

   });
Comments