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

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

นี่คือบริการขั้นสูงที่ต้อง เปิดใช้ก่อนใช้งาน.

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

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

หากต้องการรายงานปัญหาและดูการสนับสนุนอื่นๆ โปรดดู คู่มือการสนับสนุน 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);
  }
}

รับข้อมูลบุคคลสำหรับผู้ใช้

หากต้องการรับโปรไฟล์ของผู้ใช้ คุณต้องขอ ขอบเขต https://www.googleapis.com/auth/userinfo.profile โดยทำตาม วิธีการเพิ่มขอบเขตที่ชัดเจน ลงในไฟล์ Manifest appsscript.json เมื่อเพิ่มขอบเขตแล้ว คุณจะใช้โค้ดต่อไปนี้ได้

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