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