Tweet to my channel using Google Apps Script

As the name implies, my goal is to send a tweet from script.gs . Twitter will be sent to my channel, ideally, without me, to visit the Twitter site.

I wrote two main functions:

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'); } 

After about a day of inexorable hacking, I was able to get the first oAuth() function to work. These are the logs, well, my user data. However, for the life of me, I cannot understand what the request holds. I get this error: Request failed for returned code 403. Truncated server response: {"errors":[{"message":"SSL is required","code":92}]} . At Google, this is not much affected. I guess the problem is somewhere in Roptions . Any help would be appreciated and I can try to provide further clarification if necessary.

+3
source share
2 answers

Eureka! Here is the solution. The irony is that I used to have something like this, but rejected it. Turns out https was my biggest problem. Today I will be feasting a modest cake.

script send tweet

 //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'); } 

When you register your Twitter application, you should check the Allow this application to be used to Sign in with Twitter option. This prevents continuous authorization pop-ups. Also, tweet text cannot contain single quotes ( ' ).

+6
source

@ J148, oauthConfig is deprived and you can no longer use it;

Now for twitter you should use OAuth1 for Script applications. Migration documents: https://developers.google.com/apps-script/migration/oauth-config?utm_campaign=oauth-appsscript-315&utm_source=gadbc&utm_medium=blog

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

To do a sample job, you need:

  • Add "OAuth1 for Script library applications" to your Script project
  • Declare any "callback url" stub in your Twitter app settings.
0
source

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


All Articles