高级人员服务

Apps 脚本中 Google 用户的数据。

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

这是一项高级服务,必须先 启用才能使用

参考

如需详细了解此服务,请参阅 People API 的参考文档。 与 Apps 脚本中的所有高级服务一样,高级 People 服务使用的对象、方法和参数均与公共 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);
  }
}

获取用户的联系人

如需获取用户个人资料,您需要请求 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);
  }
}