(我已经阅读了Tweet using Google Script中提供的解决方案,但对我而言不起作用。请不要删除此问题。)
页面 https://ctrlq.org/code/19995-google-script-to-twitter显示了一种在Google脚本中创建推文的方法。 我只是粘贴了那里提供的代码,并成功遵循了以下说明:
现在,当我运行代码时,几乎没有任何反应,但是出现以下错误消息: ReferenceError: "twit" is not defined. (line 22, file "Code")
。
function sendTweet(status){
status = status || "I found this snippet on @labnol's ctrlq.org";
var twitterKeys= {
TWITTER_CONSUMER_KEY: "1fZn8cBR...",
TWITTER_CONSUMER_SECRET: "588gJxh...",
TWITTER_ACCESS_TOKEN: "112c5e0...",
TWITTER_ACCESS_SECRET: "DcvV614.",
};
var props = PropertiesService.getScriptProperties();
props.setProperties(twitterKeys);
var service = new Twitter.OAuth(props);
if ( service.hasAccess() ) {
var response = twit.sendTweet(status); // ← THIS IS LINE 21
if (response) {
Logger.log("Tweet ID " + response.id_str);
} else {
// Tweet could not be sent
// Go to View -> Logs to see the error message
}
}
}
请告诉我如何修复 此特定代码 。
在https://medium.com/javascript-in-plain-english/i-made-a-twitter-bot-from-nothing-but-a-google-sheet-ef0ba6e1b194中找到了答案,只需在 var service = new Twitter.OAuth(props);
之后添加缺少的变量:
var twit = new Twitter.OAuth(props);
最终代码如下:
function sendTweet(){
var status = "Text to be tweeted goes here.";
var twitterKeys= {
TWITTER_CONSUMER_KEY: "1fZBR...",
TWITTER_CONSUMER_SECRET: "Ig...tzpa4E...",
TWITTER_ACCESS_TOKEN: "1125...90...",
TWITTER_ACCESS_SECRET: "...22GrDcv...",
};
var props = PropertiesService.getScriptProperties();
props.setProperties(twitterKeys);
var service = new Twitter.OAuth(props);
var twit = new Twitter.OAuth(props); // ← THIS WAS THE MISSING VARIABLE
if ( service.hasAccess() ) {
var response = twit.sendTweet(status);
if (response) {
Logger.log("Tweet ID " + response.id_str);
} else { }
}
}