【问题标题】:Google App Scripts / TwitterLib getting error Exception when trying to post a tweetGoogle App Scripts / TwitterLib 在尝试发布推文时出现错误异常
【发布时间】:2022-12-04 14:36:39
【问题描述】:

Trying to implement TwitterLib to send tweets from Google Sheets. I am using Google App Scripts, and the following code -

var sheet = SpreadsheetApp.getActive().getSheetByName(\'View\');
var startRowNumber = 1;
var endRowNumber = sheet.getLastRow();

function sendTweets() {
  var twitterKeys = {
      TWITTER_CONSUMER_KEY: \"xxxxxxxxxxxxxxxxxxx\",
      TWITTER_CONSUMER_SECRET: \"xxxxxxxxxxxxxxxxxxx\",
      TWITTER_ACCESS_TOKEN: \"xxxxxxxxxxxxxxxxxxx\",
      TWITTER_ACCESS_SECRET: \"xxxxxxxxxxxxxxxxxxx\"
  }
  
  var props = PropertiesService.getScriptProperties();
  props.setProperties(twitterKeys);
  var params = new Array(0);
  var service = new Twitterlib.OAuth(props);

  var quote;
  var identifier;
  
  for (var currentRowNumber = startRowNumber; currentRowNumber <= endRowNumber; currentRowNumber++) {
        var row = sheet.getRange(currentRowNumber + \":\" + currentRowNumber).getValues();
        // check that the second column (Date) is equal to today
        if (isToday(row[0][1])) {
            quote = row[0][0];
            identifier = currentRowNumber - 1;
          if (!service.hasAccess()) {
            console.log(\"Authentication Failed\");
          } else {
            console.log(\"Authentication Successful\");      
            var status = quote + \"\\n\\n\" + \"#Quotes #Motivation\";
            try {
              var response = service.sendTweet(status, params);
              console.log(response);
            } catch (e) {   console.log(e)  }
          }
          break;
        }
    } 
  
}

function isToday(date) {
    var today = new Date();
    var dateFromRow = new Date(date);
    return dateFromRow.getDate() == today.getDate() &&
        dateFromRow.getMonth() == today.getMonth() &&
        dateFromRow.getFullYear() == today.getFullYear()
}

I have signed up for Twitter DEV and have my API Key and Secret (CONSUMER_KEY and CONSUMER_SECRET above) and Access Token and Access Secret as well. I have turned on OAuth 1.0a, Read and write and Direct Message selected, and a Callback URI of (https://script.google.com/macros/s/YOUR_SCRIPT_ID/usercallback) - Replacing YOUR_SCRIPT_ID with the actual one I have.

I am seeing an Authentication Successful message in my Console, but seeing this error when running inside Apps Scripts IDE:

Send tweet failure. Error was: {\"name\":\"Exception\"}

Nothing more. I am not sure what else to check to see what I am doing wrong. Any help or resources to read over would greatly be appreciated! Thank you so much!

JJ

  • Does your app have Essential Access, or Elevated Access? You will need elevated access to use the V1.1 API which is probably what the Google Script is using.

标签: google-apps-script google-sheets twitter


【解决方案1】:
猜你喜欢
  • 2021-10-04
  • 2012-01-13
  • 2015-11-14
  • 2014-07-11
  • 2016-08-13
  • 2021-03-26
  • 2013-06-11
  • 2013-06-01
  • 2022-01-09
相关资源
最近更新 更多