Meteor,Access Google contacts of loggedin user

2019-08-05 04:40发布

问题:

How to get all contacts of a loggedin user,i added request permission to access google contacts

requestPermissions: {
        facebook: ['email', 'user_friends','read_friendlists'],
         google:['https://www.google.com/m8/feeds']
    },

I don't know how to access contacts,i tried to google it but i'm confused with the results.

Does anyone know how to do this?

can anyone point me to the answer?

I really appreciate any help

回答1:

I Just used google-contacts atmosphere package and write the following function in server side and it worked for me

var opts= { email: Meteor.user().services.google.email,
              consumerKey: "APIKEY",
              consumerSecret: "APPSECRET",
              token: Meteor.user().services.google.accessToken,
              refreshToken: Meteor.user().services.google.refreshToken};

            gcontacts = new GoogleContacts(opts);

            gcontacts.refreshAccessToken(opts.refreshToken, function (err, accessToken)
             {
                if(err)
                {
                    console.log ('gcontact.refreshToken, ', err);
                    return false;
                }
                else
                {
                    console.log ('gcontact.access token success!');
                    gcontacts.token = accessToken;
                    gcontacts.getContacts(function(err, contact) 
                    {
                                  \\here i am able to access all contacts
                      console.log(contact);
                    })

                }
             });

To do this,you need a refreshToken from google,by default meteor will not get it for you.

Add the following code in client side to get the refreshtoken(requestOfflineToken)

Accounts.ui.config({
    requestPermissions: {
        facebook: ['email', 'user_friends','read_friendlists'],
         google:['https://www.google.com/m8/feeds']
    },
    requestOfflineToken: {
      google: true
  },
  passwordSignupFields: 'USERNAME_AND_EMAIL'
});

NOTE: To work with contacts API you need to enable contacts api in your google console.

Hope it may help someone out there.