高级人员服务

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

获取用户对应的个人

如需获取用户个人资料,您需要按照有关向 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);
  }
}