Change Twitter Profile Picture (Avatar) with Google Scripts

C
Published in: Google Apps Script - Twitter

This Google Script updates the Twitter user’s profile image or avatar. Twitter asynchronously processes the uploaded file before updating the user’s profile image URL.

function oAuthConfig() {

  var oAuthConfig = UrlFetchApp.addOAuthService("twitter");
  oAuthConfig.setAccessTokenUrl("http://api.twitter.com/oauth/access_token");
  oAuthConfig.setRequestTokenUrl("http://api.twitter.com/oauth/request_token");
  oAuthConfig.setAuthorizationUrl("http://api.twitter.com/oauth/authorize");

  oAuthConfig.setConsumerKey("PUT CONSUMER KEY HERE");
  oAuthConfig.setConsumerSecret("PUT CONSUMER SECRET HERE");
}

function setProfileImage() {

  oAuthConfig();

  // This is a picture that will be set as Twitter avatar
  var picture = UrlFetchApp.fetch("https://twitter.com/image.png");
  var encodedImage = Utilities.base64Encode(picture.getContent());

  var options =
  {
    "method": "post",
    "oAuthServiceName" : "twitter",
    "oAuthUseToken" : "always",
    "payload": { "image" : encodedImage, "skip_status": true}
  };

  var request = UrlFetchApp.fetch("http://api.twitter.com/1.1/account/update_profile_image.json", options);
}
Published in: Google Apps Script - Twitter

Looking for something? Find here!

Meet the Author

Web Geek, Tech Columnist
A
Amit Agarwal

Amit Agarwal is a Google Developer Expert in GSuite and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. Read more on Lifehacker and YourStory

Get in touch