使用Google Apps脚本向我的Feed发送推文

时间:2014-08-30 00:38:46

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

正如标题所示,我的目标是能够从script.gs发送推文。该推文将发布到我的Feed中,理想情况下我无需访问Twitter网站。

我写了两个主要功能来尝试这个:

script.gs

//post tweet
function oAuth() {
  var CONSUMER_KEY = "**********************";
  var CONSUMER_SECRET = "*************************************************";
  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/authorize");
  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 = "Tweet";
  var Roptions = {
    method: "post",
    oAuthServiceName: "twitter",
    oAuthUseToken: "always",
    status: status
  };
  var url = "https://api.twitter.com/1.1/statuses/update.json";
  Logger.log('begin post');
  var request = UrlFetchApp.fetch(url, Roptions); //the trouble line. Execution stops.
  Logger.log('post complete');
}

经过大约一天无情的黑客攻击后,我能够获得第一个功能oAuth()。那记录了我的用户数据。然而,对于我的生活,我无法弄清楚是什么阻碍request。我确实收到了这个错误:Request failed for returned code 403. Truncated server response: {"errors":[{"message":"SSL is required","code":92}]}。谷歌搜索这个并没有多少。我猜这个问题出现在Roptions的某个地方。任何帮助将不胜感激,如果需要,我可以尝试进一步澄清。

2 个答案:

答案 0 :(得分:6)

尤里卡!这是解决方案。具有讽刺意味的是,我以前曾经有类似的东西,但已经解雇了它。事实证明https是我最大的问题。今晚我会吃谦卑的馅饼。

发送推文的脚本

//post tweet
function oAuth() {
  var CONSUMER_KEY = "*************************";
  var CONSUMER_SECRET = "**************************************************";
  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='Test tweet';
  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');
}

当您注册Twitter应用时,您必须选中Allow this application to be used to Sign in with Twitter选项。这可以防止持续的授权弹出窗口。此外,推文文本不能包含单引号(')。

答案 1 :(得分:0)

@ J148,oauthConfig已经删除,你不能再使用它了;

现在,对于Twitter,您必须使用OAuth1 for Apps Script。迁移文档: https://developers.google.com/apps-script/migration/oauth-config?utm_campaign=oauth-appsscript-315&utm_source=gadbc&utm_medium=blog

样品: https://github.com/googlesamples/apps-script-oauth1/blob/master/samples/Twitter.gs

要使样本正常工作,您必须:

  • 将“OAuth1 for Apps脚本库”添加到您的脚本项目
  • 在Twitter应用程序的设置中声明一些存根“回调网址”