高级人员服务

通过高级“联系人”服务,您可以使用 Apps 脚本中的 People API。此 API 允许 创建、读取和更新已登录用户的联系人数据并读取个人资料 Google 用户数据。

参考

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

如需报告问题和寻求其他支持,请参阅 人员 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);
  }
}

为用户获取联系人

获取用户的个人资料,您需要 https://www.googleapis.com/auth/userinfo.profile 作用域(通过遵循 添加显式作用域的说明 添加到 appsscript.json 清单文件中。添加范围后,您可以使用 以下代码:

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);
  }
}