Post an Update to Twitter with Google Apps Script

You can use Google Apps Script to post status updates (tweets) to Twitter.

This example generates an OAuth 1.0a HMAC-SHA1 signature that is converted to base64 and is passed to the Twitter API via an HTTP POST request. You can use it to send tweets from Google Addons, Google Sheets and standalone web apps.

 * Post to Twitter from Google Apps Script
 * Code by @rcknr

function postTweet(message) {
  var method = 'POST';
  var baseUrl = '';
  var props = PropertiesService.getScriptProperties();

  var oauthParameters = {
    oauth_consumer_key: props.getProperty('CONSUMER_KEY'),
    oauth_token: props.getProperty('ACCESS_TOKEN'),
    oauth_timestamp: Math.floor(new Date().getTime() / 1000).toString(),
    oauth_signature_method: 'HMAC-SHA1',
    oauth_version: '1.0',

  oauthParameters.oauth_nonce = oauthParameters.oauth_timestamp + Math.floor(Math.random() * 100000000);

  var payload = {
    status: message,

  var queryKeys = Object.keys(oauthParameters).concat(Object.keys(payload)).sort();

  var baseString = queryKeys.reduce(function (acc, key, idx) {
    if (idx) acc += encodeURIComponent('&');
    if (oauthParameters.hasOwnProperty(key)) acc += encode(key + '=' + oauthParameters[key]);
    else if (payload.hasOwnProperty(key)) acc += encode(key + '=' + encode(payload[key]));
    return acc;
  }, method.toUpperCase() + '&' + encode(baseUrl) + '&');

  oauthParameters.oauth_signature = Utilities.base64Encode(
      props.getProperty('CONSUMER_SECRET') + '&' + props.getProperty('ACCESS_SECRET')

  var options = {
    method: method,
    headers: {
        'OAuth ' +
          .reduce(function (acc, key) {
            acc.push(key + '="' + encode(oauthParameters[key]) + '"');
            return acc;
          }, [])
          .join(', '),
    payload: Object.keys(payload)
      .reduce(function (acc, key) {
        acc.push(key + '=' + encode(payload[key]));
        return acc;
      }, [])
    muteHttpExceptions: true,

  var response = UrlFetchApp.fetch(baseUrl, options);
  var responseHeader = response.getHeaders();
  var responseText = response.getContentText();

function encode(string) {
  return encodeURIComponent(string)
    .replace('!', '%21')
    .replace('*', '%2A')
    .replace('(', '%28')
    .replace(')', '%29')
    .replace("'", '%27');
Amit Agarwal

Amit Agarwal

Google Developer Expert, Google Cloud Champion

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

Amit has developed several popular Google add-ons including Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory


Awards & Titles

Digital Inspiration has won several awards since it's launch in 2004.

Google Developer Expert

Google Developer Expert

Google awarded us the Google Developer Expert award recogizing our work in Google Workspace.

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards in 2017.

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional (MVP) title for 5 years in a row.

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator title recognizing our technical skill and expertise.

Email Newsletter

Sign up for our email newsletter to stay up to date.

We will never send any spam emails. Promise.