【JavaScript実験】 Google Apps ScriptでTwitterの検索結果を取得する

さしあたりコードのみ。

function app() {
  var properties = PropertiesService.getScriptProperties();
  var KEY = properties.getProperty("TWITTER_API_KEY");
  var SECRET = properties.getProperty("TWITTER_API_SECRET");
  
  var credentials = createCredentials(KEY, SECRET);
  var token = getToken(credentials);
  var statuses = getTweets(token, "#okinawa");
  
  statuses.forEach(function(status) {
    Logger.log(status.text);
  });
  
  function createCredentials(key, secret) {
    return Utilities.base64Encode(key + ":" + secret);
  }
  
  function getToken(credentials) {
    var response = UrlFetchApp.fetch("https://api.twitter.com/oauth2/token", {
      "method": "post",
      "headers": {
        "Authorization": "Basic " + credentials
      },
      "payload": {
        "grant_type": "client_credentials"
      }
    });
    
    return JSON.parse(response.getContentText()).access_token;
  }
  
  function getTweets(token, query) {
    var response = UrlFetchApp.fetch("https://api.twitter.com/1.1/search/tweets.json?q=" + encodeURIComponent(query), {
      "method": "get",
      "headers": {
        "Authorization": "Bearer " + token
      }
    });
    
    return JSON.parse(response.getContentText()).statuses;
  }
}