高级人员服务

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

参考

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

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

示例代码

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

获取用户的连接

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

高级/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 范围。添加范围后,您可以使用以下代码:

高级/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 帐号的用户信息,请使用以下代码:

高级/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);
  }
}