Twitter (OAuth1.0)

Retrieve tweets for a specified user

// User-level Twitter API request
// Requires the OAuth1 library to be pasted into the script.
// https://developers.google.com/adwords/scripts/docs/examples/oauth10-library

var CONSUMER_KEY = 'INSERT_CONSUMER_KEY_HERE';
var CONSUMER_SECRET = 'INSERT_CONSUMER_SECRET_HERE';
var ACCESS_TOKEN = 'INSERT_ACCESS_TOKEN_HERE';
var ACCESS_SECRET = 'INSERT_ACCESS_SECRET_HERE';

/**
 * Retrieves the tweets for the specified Twitter user.
 * @param {string} screenName The Twitter name of the user, e.g. 'sundarpichai'
 * @return {?Object} The complex response object containing tweets, or null if
 *     failure. See https://dev.twitter.com/rest/reference/get/statuses/user_timeline
 *     for structure of this object.
 */
function getTweetsForUser(screenName) {
  if (typeof OAuth1 === 'undefined') {
    var libUrl = 'https://developers.google.com/adwords/scripts/docs/examples/oauth10-library';
    throw Error('OAuth1 library not found. Please take a copy of the OAuth1 ' +
        'library from ' + libUrl + ' and append to the bottom of this script.');
  }
  var params = {screen_name: screenName};
  var authUrlFetch = OAuth1.withAccessToken(CONSUMER_KEY, CONSUMER_SECRET,
      ACCESS_TOKEN, ACCESS_SECRET);
  var response = authUrlFetch
      .fetch('https://api.twitter.com/1.1/statuses/user_timeline.json', params);
  var responseText = response.getContentText();
  return JSON.parse(responseText);
}

// Paste the OAuth1 library here.

Send a tweet

// User-level Twitter API request
// Requires the OAuth1 library to be pasted into the script.
// https://developers.google.com/adwords/scripts/docs/examples/oauth10-library

var CONSUMER_KEY = 'INSERT_CONSUMER_KEY_HERE';
var CONSUMER_SECRET = 'INSERT_CONSUMER_SECRET_HERE';
var ACCESS_TOKEN = 'INSERT_ACCESS_TOKEN_HERE';
var ACCESS_SECRET = 'INSERT_ACCESS_SECRET_HERE';

/**
 * Sends a tweet.
 * @param {string} message The message to send.
 * @return {?Object} The complex response object with the status of the send
 *     request. See https://dev.twitter.com/rest/reference/post/statuses/update
 *     for the structure of this object.
 */
function sendTweet(message) {
  if (typeof OAuth1 === 'undefined') {
    var libUrl = 'https://developers.google.com/adwords/scripts/docs/examples/oauth10-library';
    throw Error('OAuth1 library not found. Please take a copy of the OAuth1 ' +
        'library from ' + libUrl + ' and append to the bottom of this script.');
  }
  var params = '';
  var tweet = message.substring(0, 160);
  var options = {method: 'POST', payload: {status: tweet}};
  var authUrlFetch = OAuth1.withAccessToken(CONSUMER_KEY, CONSUMER_SECRET,
      ACCESS_TOKEN, ACCESS_SECRET);
  var response = authUrlFetch
      .fetch('https://api.twitter.com/1.1/statuses/update.json', params,
      options);
  var responseText = response.getContentText();
  return JSON.parse(responseText);
}

Send feedback about...

AdWords Scripts
AdWords Scripts
Need help? Visit our support page.