Layanan Orang Lanjutan

Layanan Orang tingkat lanjut memungkinkan Anda menggunakan People API di Apps Script. API ini memungkinkan skrip membuat, membaca, dan memperbarui data kontak untuk pengguna yang login dan membaca data profil untuk pengguna Google.

Referensi

Untuk informasi mendetail tentang layanan ini, lihat dokumentasi referensi untuk People API. Seperti semua layanan lanjutan di Apps Script, layanan Orang tingkat lanjut menggunakan objek, metode, dan parameter yang sama seperti API publik. Untuk informasi selengkapnya, lihat Cara tanda tangan metode ditentukan.

Untuk melaporkan masalah dan menemukan dukungan lainnya, lihat Panduan dukungan Orang v1.

Kode contoh

Kode contoh di bawah ini menggunakan API versi 1.

Mendapatkan koneksi pengguna

Untuk mendapatkan daftar orang dalam kontak pengguna, gunakan kode berikut:

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

Mengetahui identitas pengguna

Untuk mendapatkan profil pengguna, Anda harus meminta cakupan https://www.googleapis.com/auth/userinfo.profile dengan mengikuti petunjuk untuk menambahkan cakupan eksplisit ke file manifes appsscript.json. Setelah cakupan ditambahkan, Anda dapat menggunakan kode berikut:

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

Dapatkan orang untuk Akun Google

Guna mendapatkan informasi orang untuk Akun Google apa pun, gunakan kode berikut:

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