PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, November 6, 2022

[FIXED] How to set or update a contact's photo with Google Apps Script?

 November 06, 2022     contacts, google-apps-script, google-contacts-api     No comments   

Issue

I used this other question as an example, and it appears like it works, but the contact's photo doesn't actually change, and no error is returned.

I'm able to get the contact's current photo and delete the contact's photo just fine.

My Code

const accessToken = ScriptApp.getOAuthToken();
const id = '4c18faa28828aa3f';
const url = '-URL Omitted-';
const blob = UrlFetchApp.fetch(url).getBlob();
const data = Utilities.base64EncodeWebSafe(blob.getBytes());
const response = UrlFetchApp.fetch(`https://www.google.com/m8/feeds/photos/media/me/${id}`, {
  method: 'put',
  contentType: 'image/jpeg',
  payload: data,
  headers: {
    Authorization: `Bearer ${accessToken}`,
  },
});
const content = response.getContentText();
console.log(content);

Response XML

<?xml version='1.0' encoding='UTF-8'?>
<entry xmlns='http://www.w3.org/2005/Atom'>
  <id>http://www.google.com/m8/feeds/photos/media/-OMITTED-%40gmail.com/4c18faa28828aa3f</id>
  <updated>2020-10-12T22:10:01.271Z</updated>
  <link rel='self' type='application/atom+xml'
        href='https://www.google.com/m8/feeds/photos/media/-OMITTED-%40gmail.com/4c18faa28828aa3f'/>
  <link rel='edit' type='application/atom+xml'
        href='https://www.google.com/m8/feeds/photos/media/-OMITTED-%40gmail.com/4c18faa28828aa3f/1B2M2Y8AsgTpgAmY7PhCfg'/>
</entry>

Solution

I figured it out. I was encoding the image blob bytes for the payload for some reason. I should have just passed the blob as the payload like this.

const accessToken = ScriptApp.getOAuthToken();
const id = '4c18faa28828aa3f';
const url = '-URL Omitted-';
const blob = UrlFetchApp.fetch(url).getBlob();
const response = UrlFetchApp.fetch(`https://www.google.com/m8/feeds/photos/media/me/${id}`, {
  method: 'put',
  contentType: 'image/jpeg',
  payload: blob,
  headers: {
    Authorization: `Bearer ${accessToken}`,
  },
});
const content = response.getContentText();
console.log(content);


Answered By - burkybang
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing