บริการบุคคลขั้นสูง

บริการ People ขั้นสูงจะช่วยให้คุณใช้ People API ใน Apps Script ได้ API นี้อนุญาตให้สคริปต์สร้าง อ่าน และอัปเดตข้อมูลรายชื่อติดต่อสำหรับผู้ใช้ที่ลงชื่อเข้าสู่ระบบและอ่านข้อมูลโปรไฟล์สำหรับผู้ใช้ Google

ข้อมูลอ้างอิง

สำหรับข้อมูลโดยละเอียดเกี่ยวกับบริการนี้ โปรดดูเอกสารอ้างอิงสำหรับ People API เช่นเดียวกับบริการขั้นสูงทั้งหมดใน Apps Script บริการ People ขั้นสูงจะใช้ออบเจ็กต์ เมธอด และพารามิเตอร์เดียวกันกับ API สาธารณะ โปรดดูข้อมูลเพิ่มเติมที่หัวข้อวิธีกำหนดลายเซ็นของเมธอด

หากต้องการรายงานปัญหาและค้นหาการสนับสนุนอื่นๆ โปรดดูคู่มือการสนับสนุนสำหรับแอปพลิเคชัน People v1

รหัสตัวอย่าง

โค้ดตัวอย่างด้านล่างใช้ API เวอร์ชัน 1

รับการเชื่อมต่อของผู้ใช้

หากต้องการดูรายชื่อบุคคลในรายชื่อติดต่อของผู้ใช้ ให้ใช้รหัสต่อไปนี้

ขั้นสูง/บุคคล.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 โดยทำตามวิธีการเพิ่มขอบเขตอย่างชัดแจ้งไปยังไฟล์ Manifest appsscript.json เมื่อเพิ่มขอบเขตแล้ว คุณจะใช้โค้ดต่อไปนี้ได้

ขั้นสูง/บุคคล.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 ให้ใช้รหัสต่อไปนี้

ขั้นสูง/บุคคล.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);
  }
}