Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tweet using Google Script

I pulled this code from this question.

Applying the author's solution, I am always given this error: enter image description here

I get my key and secret from my created twitter app here:

enter image description here

I have the app configured to write...

What am I doing wrong?

//post tweet
function oAuth() {
  var CONSUMER_KEY = "xxxx";
  var CONSUMER_SECRET = "xxxxx";
  ScriptProperties.setProperty("TWITTER_CONSUMER_KEY", CONSUMER_KEY);
  ScriptProperties.setProperty("TWITTER_CONSUMER_SECRET", CONSUMER_SECRET);
  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/authenticate");
  oauthConfig.setConsumerKey(ScriptProperties.getProperty("TWITTER_CONSUMER_KEY"));
  oauthConfig.setConsumerSecret(ScriptProperties.getProperty("TWITTER_CONSUMER_SECRET"));
  var options = {muteHttpExceptions: true,oAuthServiceName:'twitter',oAuthUseToken:'always'}
  var url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
  var response = UrlFetchApp.fetch(url, options).getContentText();
  Logger.log(response);
}
function postTweet() {
  oAuth();
  Logger.log('oAuth complete');
  var status='Operational!';
  var options = {
    "method": "post",
    "oAuthServiceName": "twitter",
    "oAuthUseToken": "always",
    "payload":{"status":status}
  };
  var url = "https://api.twitter.com/1.1/statuses/update.json";
  Logger.log('begin post');
  var request = UrlFetchApp.fetch(url, options);
  Logger.log('post complete');
}
like image 421
jth41 Avatar asked Oct 21 '22 01:10

jth41


1 Answers

I was getting this error also, until I realized you need to specify a 'CallBack URL' in Twitter:

Twitter CallBack URL

Specifying that as either 'https://script.google.com' or 'https://script.google.com/macros' is allowing me to Authorize. I've tested this and it's currently letting me post with the code that you've listed.

One note however if you try and post the same 'status' text twice, it will throw you the following error:

Duplicate Status Error

This isn't an issue as you simply change the value of the variable 'Status', but it threw me the first time.

like image 90
HDCerberus Avatar answered Oct 23 '22 22:10

HDCerberus