是否可以使用谷歌的人api加载联系人图片?

时间:2017-12-18 23:33:07

标签: node.js google-api google-api-nodejs-client google-people

我试图找到适合Google的People API的方法。到目前为止,我已经能够使用

加载联系人组
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;
};

其中people是实例化的google-api-nodejs-client people(v1)对象。

我正在尝试为用户的联系人获取个人资料照片。如何为每个联系人加载公开个人资料图片或占位符?

1 个答案:

答案 0 :(得分:1)

是的,可以使用Google的人员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;
  }
});