進階使用者服務

進階 People 服務可讓您在 Apps Script 中使用 People API。這個 API 可讓指令碼建立、讀取及更新已登入使用者的聯絡人資料,並讀取 Google 使用者的個人資料。

參考資料

如要進一步瞭解這項服務,請參閱 People API 的參考說明文件。與 Apps Script 中的所有進階服務一樣,進階 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);
  }
}