שירות אנשים מתקדם

נתונים של משתמשי Google ב-Apps Script.

השירות המתקדם 'אנשים' מאפשר לכם להשתמש ב-People API ב-Google Apps Script. ה-API הזה מאפשר לסקריפטים ליצור, לקרוא ולעדכן נתוני אנשי קשר של המשתמש שמחובר לחשבון, ולקרוא נתוני פרופיל של משתמשי Google.

זהו שירות מתקדם שצריך להפעיל לפני השימוש.

חומרי עזר

מידע מפורט על השירות הזה מופיע במאמרי העזרה של People API. בדומה לכל השירותים המתקדמים ב-Apps Script, שירות People המתקדם משתמש באותם אובייקטים, שיטות ופרמטרים כמו ממשק ה-API הציבורי. מידע נוסף זמין במאמר איך נקבעות חתימות של שיטות.

כדי לדווח על בעיות ולקבל תמיכה נוספת, אפשר לעיין במדריך התמיכה של People v1.

קוד לדוגמה

בדוגמת הקוד הבאה נעשה שימוש בגרסה 1 של ה-API.

קבלת הקשרים של המשתמש

כדי לקבל רשימה של אנשים באנשי הקשר של המשתמש, משתמשים בקוד הבא:

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. לשם כך, פועלים לפי ההוראות להוספת היקפי הרשאות מפורשים לקובץ המניפסט 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);
  }
}