高级人员服务

借助高级通讯录服务,您可以在 Apps 脚本中使用 People API。借助此 API,脚本可以创建、读取和更新已登录用户的联系人数据,以及读取 Google 用户的个人资料数据。

参考

如需详细了解此服务,请参阅 People API 的参考文档。与 Apps 脚本中的所有高级服务一样,高级“联系人”服务使用的对象、方法和参数均与公共 API 相同。如需了解详情,请参阅如何确定方法签名

如需报告问题和寻求其他支持,请参阅 People v1 支持指南

示例代码

以下示例代码使用该 API 的版本 1

获取用户的连接

如需获取用户通讯录中的联系人列表,请使用以下代码:

advanced/people.gs
/**
 * Gets a list of people in the user's contacts.
 * @see https://developers.google.com/people/api/rest/v1/people.connections/list
 */
function getConnections() {
  try {
    // Get the list of connections/contacts of user's profile
    const people = People.People.Connections.list('people/me', {
      personFields: 'names,emailAddresses'
    });
    // Print the connections/contacts
    console.log('Connections: %s', JSON.stringify(people, null, 2));
  } catch (err) {
    // TODO (developers) - Handle exception here
    console.log('Failed to get the connection with an error %s', err.message);
  }
}

获取用户的人员

如需获取用户的个人资料,您需要按照有关向 appsscript.json 清单文件添加显式镜重范围的说明,请求 https://www.googleapis.com/auth/userinfo.profile 镜重范围。添加镜重范围后,您可以使用以下代码:

advanced/people.gs
/**
 * Gets the own user's profile.
 * @see https://developers.google.com/people/api/rest/v1/people/getBatchGet
 */
function getSelf() {
  try {
    // Get own user's profile using People.getBatchGet() method
    const people = People.People.getBatchGet({
      resourceNames: ['people/me'],
      personFields: 'names,emailAddresses'
      // Use other query parameter here if needed
    });
    console.log('Myself: %s', JSON.stringify(people, null, 2));
  } catch (err) {
    // TODO (developer) -Handle exception
    console.log('Failed to get own profile with an error %s', err.message);
  }
}

获取 Google 账号的用户

如需获取任何 Google 账号的个人信息,请使用以下代码:

advanced/people.gs
/**
 * Gets the person information for any Google Account.
 * @param {string} accountId The account ID.
 * @see https://developers.google.com/people/api/rest/v1/people/get
 */
function getAccount(accountId) {
  try {
    // Get the Account details using account ID.
    const people = People.People.get('people/' + accountId, {
      personFields: 'names,emailAddresses'
    });
    // Print the profile details of Account.
    console.log('Public Profile: %s', JSON.stringify(people, null, 2));
  } catch (err) {
    // TODO (developer) - Handle exception
    console.log('Failed to get account with an error %s', err.message);
  }
}