LoginSignup
27
25

More than 5 years have passed since last update.

Google Spreadsheetから定期的にtwitterにつぶやくbotを作る

Last updated at Posted at 2015-01-10

oAuthConfig クラスが 2015/07/06 で使えなくなりました! あとでこの記事編集しますが、待てない方は以下を参照して改造してください。
Migrating from OAuthConfig to the OAuth1 library

以下、動きません (2015/07/06)

掲題の通りです。

基本的に GAS(Google Apps Script)でお手軽にbotを作る方法。を参照すればOKなんですが、コードがちょっと古く、廃止になったAPIを使っているので、2015年1月時点で警告がでないようにコードを改変しました。

備忘のために載せておきます。

function tweetInitialize() {
  // Setup OAuthServiceConfig
  var oAuthConfig = UrlFetchApp.addOAuthService("twitter");
  oAuthConfig.setAccessTokenUrl("https://api.twitter.com/oauth/access_token");
  oAuthConfig.setRequestTokenUrl("https://api.twitter.com/oauth/request_token");
  oAuthConfig.setAuthorizationUrl("https://api.twitter.com/oauth/authorize");
  var scriptProperties = PropertiesService.getScriptProperties();
  oAuthConfig.setConsumerKey(scriptProperties.getProperty("twitterConsumerKey"));
  oAuthConfig.setConsumerSecret(scriptProperties.getProperty("twitterConsumerSecret"));
}

function twitterPost(text) {
  var options = {
    "oAuthServiceName" : "twitter",
    "oAuthUseToken" : "always",
    "method" : "POST"
  };
  var encodedTweet = encodeURIComponent(text);
  var result = UrlFetchApp.fetch("https://api.twitter.com/1.1/statuses/update.json?status=" + encodedTweet, options);
  var o  = JSON.parse(result.getContentText());
  Logger.log(o);
  Logger.log(result.getResponseCode());
}

27
25
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
27
25