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

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top