進階使用者服務

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

參考資料

如要進一步瞭解這項服務,請參閱 People API 的參考說明文件。如同 Apps Script 的所有進階服務,進階使用者服務會使用與公用 API 相同的物件、方法和參數。詳情請參閱「如何判定方法簽章」一文。

如要回報問題及尋找其他支援,請參閱 People v1 支援指南

程式碼範例

下方程式碼範例使用第 1 版的 API。

取得使用者之間的連結

如要取得使用者聯絡人名單,請使用下列程式碼:

進階/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);
  }
}