Joshua M. Moore
Joshua M. Moore

Reputation: 399

Is it possible to load contact pictures using google's people api?

I'm trying to find my way around Google's People API. So far, I've been able to load the contact groups using

try{
    contact_groups = await new Promise(function(resolve, reject){
        people.contactGroups.list({
            auth: oauth2Client
        }, function(error, response){
            if(!error){
                resolve(response);
            }else{
                reject(error);
            }
        });
    });
}catch(error){
    throw error;
};

where people is the instantiated google-api-nodejs-client people(v1) object.

I'm trying to get a profile picture for the user's contacts. How do I load the public profile picture or a placeholder for each of the contacts?

Upvotes: 0

Views: 363

Answers (1)

Joshua M. Moore
Joshua M. Moore

Reputation: 399

Yes, it's possible using Google's people API.

const google = require('googleapis');
const OAuth2 = google.auth.OAuth2;

var oauth2Client = new OAuth2(
  'CLIENT_ID',
  'CLIENT_SECRET'
  'http://localhost:3000/auth/google/callback'
);

router.get('/signin', function(req, res, next){
  var url = oauth2Client.generateAuthUrl({
    scope: [
      'https://www.googleapis.com/auth/contacts.readonly'
    ]
  });

  res.redirect(url);
});

router.get('/google/callback', function(req, res, next){
  oauth2Client.getToken(req.query.code, async function(err, tokens){
    if(!err){
      oauth2Client.credentials = tokens;

      try{
        var contacts = await new Promise((resolve, reject) => {
          people.people.connections.list({
            resourceName: 'people/me',
            auth: oauth2Client,
            personFields: 'names,photos'
        }, function(error, response){
          if(!error){
            resolve(response);
        }else{
          reject(error);
        }
      })
    });
  }catch(error){
    throw error;
  }
});

Upvotes: 1

Related Questions