0
votes

Update/Solution

There isn't a way to add a photo to a person object on creating a contact via People API. However you can updated the photo directly after using People.People.updateContactPhoto. updateContactPhoto has a limit of 60 queries per minute per user. If your script needs to update more than 60 contact photos, then you will need to add a delay in your code so that query limit isn't reached

Question:

I am creating a large list of contacts from a set of data, including contact photos for each contact. When I create contacts via people.createContact, if I have the photos field there it states "person.photos is a read only field." How I can I create the contact with a profile photo link as well?

Example Code

const contact = {names:..., addresses:..., emailAddressess:..., phoneNumbers:..., 
  photos: {"url":..., "primary": true}
}

People.People.createContact(contact)

this gives the error message Error Message

Update 1: Currently this is what I'm doing and it works. The main problem now is I don't want to use the Utilities.Sleep function. I'm looping over this function hundreds of times, and that adds up quickly. If I don't use the sleep though around the 40th or 50th contact it throws an "Internal error encountered". The contact it errors on is different each time, so the error is not tied to a specific person. My assumption is the error comes from running too many requests too quickly, but how can I run this without forcing it to sleep?

function importPerson(person){
  const photoData = encodePhotoData(person.photos[0].url)
  delete person.photos

  const newContact = People.People.createContact(person)
  
  People.People.updateContactPhoto({ 
    photoBytes: photoData,
  },newContact.resourceName)

  //This stops it from throwing an error
  Utilities.sleep(1000)
}
1

1 Answers

2
votes

You need to use people.updateContactPhoto to update a contact's person. You need to convert your photo's url into a raw photo bytes in base64-encoded string.

Sample Code:

  var resource = {
   names: [
     {
       givenName: "Sample1"
     }
   ],
   emailAddresses: [
     {
       value: "[email protected]"
     }
   ]
 };
  //create contact
  var response = People.People.createContact(resource);

  //Get raw photo bytes in base64-encoded string
  var url = "https://lh3.googleusercontent.com/-T_wVWLlmg7w/AAAAAAAAAAI/AAAAAAAABa8/00gzXvDBYqw/s100/photo.jpg"
  var photoBlob = UrlFetchApp.fetch(url);
  var base64EncodedBytes = Utilities.base64Encode(photoBlob.getBlob().getBytes()); 

  var photoResource = {
    photoBytes: base64EncodedBytes
  }
  //update photo
  People.People.updateContactPhoto(photoResource,response.resourceName)

Output:

enter image description here


(UPDATE)

Here is the sample code where I tried to create 100 contacts:

function testContact(){
  for(var i=0;i<100;i++){
      var resource = {
    names: [
      {
        givenName: "Sample1"
      }
    ],
    emailAddresses: [
      {
        value: "[email protected]"
      }
    ],
    photos:[
      {
        url: "https://lh3.googleusercontent.com/-T_wVWLlmg7w/AAAAAAAAAAI/AAAAAAAABa8/00gzXvDBYqw/s100/photo.jpg"
      }
    ]
  };
    Logger.log(i);
    importPerson(resource);
  }
}

function importPerson(person){
  const photoData = encodePhotoData(person.photos[0].url)
  delete person.photos

  const newContact = People.People.createContact(person)
  
  People.People.updateContactPhoto({ 
    photoBytes: photoData,
  },newContact.resourceName)

  //This stops it from throwing an error
  //Utilities.sleep(1000)
}