Search Twitter with Google Apps Script

Published in: Google Apps Script - Twitter

The Twitter Search Archiver uses the OAuth1 library for Google Apps Script to talk to the Twitter Search API. You need to include the OAuth1 library in your Google Script project, create a Callback URL and then call the authorization URL in a separate browser tab to authorize the Twitter service.

However, if you are only going to make read-only requests to Twitter - like fetching user timelines for Twitter RSS feeds or performing searches - you can do without the library as well. You won’t be able to post tweets or DMs though.

Twitter supports application-only authentication giving apps the ability to issue authenticated requests on behalf of the application itself without authenticating the specific user.

First create a new Twitter app and make a note of the Twitter consumer key and consumer secret. The Callback URL can be left blank since it is not required.

function Twitter() {

  // Encode consumer key and secret
  var tokenUrl = "";
  var tokenCredential = Utilities.base64EncodeWebSafe(

  //  Obtain a bearer token with HTTP POST request
  var tokenOptions = {
    headers : {
      Authorization: "Basic " + tokenCredential,
      "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
    method: "post",
    payload: "grant_type=client_credentials"

  var responseToken = UrlFetchApp.fetch(tokenUrl, tokenOptions);
  var parsedToken = JSON.parse(responseToken);
  var token = parsedToken.access_token;

  // Authenticate Twitter API requests with the bearer token
  var apiUrl = "";
  var apiOptions = {
    headers : {
      Authorization: 'Bearer ' + token
    "method" : "get"

  var responseApi = UrlFetchApp.fetch(apiUrl, apiOptions);

  var result = "";

  if (responseApi.getResponseCode() == 200) {

    // Parse the JSON encoded Twitter API response
    var tweets = JSON.parse(responseApi.getContentText());

    if (tweets) {

      for (var i = 0; i < tweets.length; i++) {
        var tweet = tweets[i].text;
        var date = new Date(tweets[i].created_at);
        result += "[" + date.toUTCString() + "]" + tweet + " / ";


📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Google Apps Script - Twitter

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
Amit Agarwal

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. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube