Here's the steps
- Copy the Google Spreadsheet that contains the sample code
- Register a new application with Twitter
- Update the sample code with your feed URL and twitter keys
- Test the application and optionally schedule it to run automatically
The sample code is in a script in a public google spreadsheet. Copy the spreadsheet. This link will open a new window. Click "Yes" when it prompts you to make a new copy of the document.
Register a new application with Twitter
Before you can tweet, you'll need to register a new application with Twitter. This will give you two strings that you will add to the sample code - consumer_key and consumer_secret. Once you have done this you'll be able to authorize the sample code to post to twitter on your behalf.
Visit the Twitter Developer site and create a new application. You'll need to login with your twitter account. Then you'll need to give your application a unique name, like "Tweet my_blog_domain.com", and a description. For "website" you can include your blog home page, or a page or article on your blog that describes what the application does. For the Callback URL you must specify "https://script.google.com/macros".
Before you can click "Create your twitter application" you'll also need to agree to the terms and conditions and answer a captcha. Once you have done this you'll receive a message that your application has been created. You need to change one setting for the application. By default the application is read-only. Which means it can't post a tweet on your behalf. From the settings tab change the application type access from read-only to read-write, and click the Update button.
Now go to the Details tab and find the strings alongside "Consumer key" and "Consumer secret" which you'll need in the next step.
Update the sample code with your feed URL and twitter keys
Open the copy of the spreadsheet that you created in the first step, and go to Tools > Script editor. At the top of the sample code you should see three variables that you need to change. The first is the URL for the feed that you want to post. The example I include is the feed for Google's official blog. You may want to test with this feed first before replacing it later with your own feed.
The next two variables are consumer_key and consumer_secret. Replace the values in the sample code with the values you received when you created a twitter application in the second step. Now click save.
Test the application and optionally schedule it to run automatically
Now you're ready to test your application. On the toolbar, pick "start" in the pull-down field which shows "Select function". Now click the run button. The first time you run the application you will receive an error message warning you "Error: Authorize at URL: ...". Click the "Details" button to see the full URL, and cut/paste this into a browser window, and you will be taken to a Twitter page which will prompt you to authorize your application to use your account. You need to click "Authorize App".
Once you have authorized the app with Twitter, go back and re-run the start function. This time you will receive a warning from Google, and will need to authorize the application with them. If all has gone well then in a few moments a tweet will appear in your twitter account showing the title and link to a random article from the feed. If nothing appears, then go back to the Script Editor and click View Logs. If there was an error it should show here. Try running the start function again and check the logs again. Other things to check are the values you copied for the consumer_key, consumer_secret and the feed url.
Once you have it working you can optionally schedule it to run at certain times and days. To do this, click the "Current project's triggers" button on the toolbar and then the link to create a new trigger. Choose "start" in the first field, and "Time-driven" in the second field. You can now define any schedule, for example, pick "Day timer" and "Noon-1pm" to have the application post one article a day sometime between noon and 1pm.
This is a simple example of how to create a Twitter Application with Google Apps Script to tweet old posts from a feed such as a Blogger blog. The tweets created just contain the article title and url. There are many ways it could be improved. For example, you could read the article labels from the feed and include these in the tweet as hashtags, by modifying the readFeed function. It could also use the Google Apps Script UrlShortener service, by modifying the makeTweet function. But hopefully it provides a base sample from which you can build.
If you find this article useful, or run into any problems, then please post a comment below.
Note: Sample code updated to use the new OAuth1 library, since Google has deprecated the old OAuth approach.