Getting error using Twitter lib

In Google Apps Script I have this piece of code in my project to send a tweet ( also in jsBin ):

function sendTweet(status) {

  var twitterKeys= {
    TWITTER_CONSUMER_KEY: "x",
    TWITTER_CONSUMER_SECRET: "x",
    TWITTER_ACCESS_TOKEN: "x",
    TWITTER_ACCESS_SECRET: "x"    
  };

  var props = PropertiesService.getScriptProperties();

  props.setProperties(twitterKeys);
  twit = new Twitter.OAuth(props);

  var service = new Twitter.OAuth(props);


  if ( service.hasAccess() ) {

    var response = twit.sendTweet(status);

    if (response) {

      Logger.log("Tweet ID " + response.id_str);

    } else {

      // Tweet could not be sent
      // Go to View -> Logs to see the error message

    }
  }  
}

sendTweet("test");

But the problem is that I get this error:

TypeError: Cannot read property "text" from undefined. (line 293, file "twitter", project "Twitter lib")

Line 293 from version 21 of the Twitter lib library ( MKvHYYdYA4G5JJHj7hxIcoh8V4oX7X1M_ ).

The "test" message actually gets twitter, despite this error. Does anyone know how to fix this?

+4
source share
2 answers

To repeat the error, you noticed an error:

TypeError: "" undefined. ( 293, "", "Twitter lib" )

sendTweet() , . .

/**
* Upload a tweet to Twitter with optional media.
*
* @param {string | Tweet} tweet the status text to send as a Twitter update
* @param {optional object} params any additional parameters to send as part of the update post
* @return {object} the Twitter response as an object if successful, null otherwise
*/
OAuth.prototype.sendTweet = function(tweet, params) {
  var i;
  var payload = {       //<=== 293
    "status" : (tweet.text || tweet)
  };

status, "test".

tweet :

  • text, Tweet,
  • .

, , tweet.text, , , tweet. tweet.text (.. ), TypeError .

, . , tweet text .

  • Tweet. Tweet Twitter API v1.1 , , , text, status . , status - text.

    function sendTweet(status) {
      if (typeof status === string)
        status = {text:status};
      ...
    
  • . , 294 :

    "status" : (tweet.hasOwnProperty("text") ? tweet.text : tweet)
    

    :

    "status" : (typeof tweet === 'object' ? tweet.text : tweet)
    

    , . .

+1

, Twitter Lib . @Mogsdad Twitter. , , script, Google Script.

, , script. , "" sendTweet, , Script , "" .

sendTweet , status undefined. undefined twit.sendTweet(), , .

, , - , "", :

function sendTestTweet() {
    sendTweet("test");
}
+1

Source: https://habr.com/ru/post/1623372/


All Articles