Contacts सेवा से People API की बेहतर सेवा पर माइग्रेट करना

Apps Script ने Contacts सेवा को 16 दिसंबर,2022 को बंद कर दिया है. इसके बजाय, People API की ऐडवांस सेवा का इस्तेमाल करें. People API, नए JSON प्रोटोकॉल का इस्तेमाल करता है. साथ ही, इसमें बेहतर सुविधाएं मिलती हैं. जैसे, संपर्कों को प्रोफ़ाइलों के साथ मर्ज करना.

इस गाइड का इस्तेमाल करके जानें कि Contacts सेवा के किन तरीकों का इस्तेमाल People API की ऐडवांस सेवा में नहीं किया जा सकता. यह भी जानें कि इसके बजाय किन तरीकों का इस्तेमाल किया जा सकता है. साथ ही, सामान्य टास्क माइग्रेट करने के लिए कोड के सैंपल पाएं. ज़्यादा जानकारी के लिए, Contacts API को दूसरी जगह भेजने से जुड़ी गाइड देखें.

ऐसे तरीके जिनके लिए People API में कोई विकल्प उपलब्ध नहीं है

यहां दी गई सूचियों में, Contacts सेवा के उन getContacts तरीकों के बारे में बताया गया है जिनके ज़रिए People API की ऐडवांस सेवा में संपर्कों को नहीं खोजा जा सकता. People API की ऐडवांस सेवा की मदद से, किसी संपर्क के names, nickNames, emailAddresses, phoneNumbers, और organizations फ़ील्ड के हिसाब से खोजा जा सकता है. ये फ़ील्ड, CONTACT सोर्स से लिए जाते हैं.

ऐसे तरीके जिनके लिए कोई दूसरा तरीका उपलब्ध नहीं है
  • getContactsByAddress(query)
  • getContactsByAddress(query, label)
  • getContactsByAddress(query, label)
  • getContactsByCustomField(query, label)
  • getContactsByDate(month, day, label)
  • getContactsByDate(month, day, year, label)
  • getContactsByDate(month, day, year, label)
  • getContactsByIM(query)
  • getContactsByIM(query, label)
  • getContactsByJobTitle(query)
  • getContactsByNotes(query)
  • getContactsByUrl(query)
  • getContactsByUrl(query, label)
  • getContactsByGroup(group)

यहां Contacts सेवा के उन getContacts तरीकों की सूची दी गई है जिनमें अतिरिक्त label पैरामीटर का इस्तेमाल किया जाता है. People API की ऐडवांस सेवा में मौजूद searchContacts का इस्तेमाल करके, मिलते-जुलते फ़ील्ड के हिसाब से संपर्क पाए जा सकते हैं. हालांकि, खोज को किसी खास लेबल तक सीमित नहीं किया जा सकता.

कुछ हद तक मिलते-जुलते तरीके
  • getContactsByEmailAddress(query, label)
  • getContactsByName(query, label)
  • getContactsByPhone(query, label)

People API के साथ उपलब्ध अन्य सुविधाएं

People API की ऐडवांस सेवा पर माइग्रेट करने पर, आपको People API की इन सुविधाओं का ऐक्सेस मिलता है. ये सुविधाएं, Contacts सेवा में उपलब्ध नहीं हैं:

  • डेटा सोर्स तय करना–किसी व्यक्ति के बारे में जानकारी खोजते समय, यह तय किया जा सकता है कि आपको कहां खोजना है. जैसे, Google Contacts या Google प्रोफ़ाइल.
  • क्वेरी स्ट्रिंग के हिसाब से लोगों को खोजें–आपको उन प्रोफ़ाइलों और संपर्कों की सूची मिल सकती है जो किसी खास स्ट्रिंग से मेल खाती हैं.
  • बैच में अनुरोध–स्क्रिप्ट को पूरा होने में लगने वाला समय कम करने के लिए, People API के अनुरोधों को बैच में भेजा जा सकता है.

सामान्य कामों के लिए कोड सैंपल

इस सेक्शन में, Contacts सेवा के सामान्य टास्क दिए गए हैं. कोड के सैंपल में, People API की ऐडवांस सेवा का इस्तेमाल करके टास्क बनाने का तरीका बताया गया है.

नाम के हिसाब से संपर्क ग्रुप पाना

यहां दिए गए कोड के सैंपल में, नाम के हिसाब से संपर्क ग्रुप पाने का तरीका बताया गया है. यह Contacts सेवा में getContactGroup(name) के बराबर है.

advanced/people.gs
/**
 * Gets a contact group with the given name
 * @param {string} name The group name.
 * @see https://developers.google.com/people/api/rest/v1/contactGroups/list
 */
function getContactGroup(name) {
  try {
    const people = People.ContactGroups.list();
    // Finds the contact group for the person where the name matches.
    const group = people['contactGroups'].find((group) => group['name'] === name);
    // Prints the contact group
    console.log('Group: %s', JSON.stringify(group, null, 2));
  } catch (err) {
    // TODO (developers) - Handle exception
    console.log('Failed to get the contact group with an error %s', err.message);
  }
}

ईमेल पते से संपर्क पाएं

इस कोड के उदाहरण में दिखाया गया है कि किसी संपर्क को उसके ईमेल पते से कैसे पाया जा सकता है. यह Contacts सेवा में getContact(emailAddress) के बराबर है.

advanced/people.gs
/**
 * Gets a contact by the email address.
 * @param {string} email The email address.
 * @see https://developers.google.com/people/api/rest/v1/people.connections/list
 */
function getContactByEmail(email) {
  try {
    // Gets the person with that email address by iterating over all contacts.
    const people = People.People.Connections.list('people/me', {
      personFields: 'names,emailAddresses'
    });
    const contact = people['connections'].find((connection) => {
      return connection['emailAddresses'].some((emailAddress) => emailAddress['value'] === email);
    });
    // Prints the contact.
    console.log('Contact: %s', JSON.stringify(contact, null, 2));
  } catch (err) {
    // TODO (developers) - Handle exception
    console.log('Failed to get the connection with an error %s', err.message);
  }
}

सभी संपर्क पाना

यहां दिए गए कोड सैंपल में, किसी उपयोगकर्ता के सभी संपर्कों को पाने का तरीका बताया गया है. यह Contacts सेवा में getContacts() के बराबर है.

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

किसी संपर्क का पूरा नाम पाना

यहां दिए गए कोड के सैंपल में, किसी संपर्क का पूरा नाम पाने का तरीका बताया गया है. यह Contacts सेवा में getFullName() के बराबर होता है.

advanced/people.gs
/**
 * Gets the full name (given name and last name) of the contact as a string.
 * @see https://developers.google.com/people/api/rest/v1/people/get
 */
function getFullName() {
  try {
    // Gets the person by specifying resource name/account ID
    // in the first parameter of People.People.get.
    // This example gets the person for the user running the script.
    const people = People.People.get('people/me', {personFields: 'names'});
    // Prints the full name (given name + family name)
    console.log(`${people['names'][0]['givenName']} ${people['names'][0]['familyName']}`);
  } catch (err) {
    // TODO (developers) - Handle exception
    console.log('Failed to get the connection with an error %s', err.message);
  }
}

किसी संपर्क के सभी फ़ोन नंबर पाना

नीचे दिए गए कोड के सैंपल में, किसी संपर्क के सभी फ़ोन नंबर पाने का तरीका दिखाया गया है. यह Contacts सेवा में getPhones() के बराबर है.

advanced/people.gs
/**
 * Gets all the phone numbers for this contact.
 * @see https://developers.google.com/people/api/rest/v1/people/get
 */
function getPhoneNumbers() {
  try {
    // Gets the person by specifying resource name/account ID
    // in the first parameter of People.People.get.
    // This example gets the person for the user running the script.
    const people = People.People.get('people/me', {personFields: 'phoneNumbers'});
    // Prints the phone numbers.
    console.log(people['phoneNumbers']);
  } catch (err) {
    // TODO (developers) - Handle exception
    console.log('Failed to get the connection with an error %s', err.message);
  }
}

किसी संपर्क का फ़ोन नंबर पाना

यहां दिए गए कोड के सैंपल में, किसी संपर्क के लिए कोई फ़ोन नंबर पाने का तरीका बताया गया है. यह Contacts सेवा में getPhoneNumber() के बराबर है.

advanced/people.gs
/**
 * Gets a phone number by type, such as work or home.
 * @see https://developers.google.com/people/api/rest/v1/people/get
 */
function getPhone() {
  try {
    // Gets the person by specifying resource name/account ID
    // in the first parameter of People.People.get.
    // This example gets the person for the user running the script.
    const people = People.People.get('people/me', {personFields: 'phoneNumbers'});
    // Gets phone number by type, such as home or work.
    const phoneNumber = people['phoneNumbers'].find((phone) => phone['type'] === 'home')['value'];
    // Prints the phone numbers.
    console.log(phoneNumber);
  } catch (err) {
    // TODO (developers) - Handle exception
    console.log('Failed to get the connection with an error %s', err.message);
  }
}