Twitter API and REBOL

Note: this script uses the OAuth method of authentication. For caveats about security, check out this Ars Technica article: Compromising Twitter’s OAuth security system (h/t Brian Hawley)

Before you start, you’ll need an Consumer API Key and Secret.  If you don’t already have these, you’ll need to register your own application over at Twitter’s Developers SiteBe sure that you activate the Read/Write option under your Application ‘Settings’ tab in order to post tweets.

To get going, ‘do the Twitter script as follows, switching out the tags for your key, secret (don’t worry, these won’t be sent with the request) and filepath to load/save your users (if the file doesn’t already exist, it will be created when when you use the twitter/save-users function):

do/args http://reb4.me/r/twitter context [
Consumer-Key: <your API key goes here>
Consumer-Secret: <your API secret goes here>
User-Store: <file-or-url to store users>
]

Example:

do/args http://reb4.me/r/twitter context [
Consumer-Key: #0123456789ABCDEF
Consumer-Secret: #FEDCBA9876543210
User-Store: %/Volumes/MyApps/TwitterUsers.r
]

This Rebol client currently supports a few methods of the Twitter API:

? twitter

twitter/as

Of the following API functions, only twitter/find can be used anonymously.  You can set the current user of subsequent requests using ‘as:

twitter/as "rgrebol"

If the user doesn’t exist in your user-store, the Twitter web page prompting authorization will appear in your browser window.  When you accept authorization per their instructions, Twitter will provide a PIN number — copy this number, then return to the console and paste the PIN number in at the prompt.

Once authorization is complete, you can save your user-store using the following function:

twitter/save-users

As you’d expect, this function saves your list of authorized users to the file location you used above.  It is recommended that you look after the resultant file — it is information you probably do not want to share.

twitter/save-users

You can save to a different location using the /to refinement:

twitter/save-users/to request-file

The following functions all return values in the same structure as described in the Twitter API Documentation:

twitter/find

‘find uses the search API.

twitter/find "Rebol"
twitter/find #Scotland

(Twitter API: search)

twitter/timeline

Returns a page of status updates for the current or specified user:

twitter/timeline
twitter/timeline/page 2
twitter/timeline/for "rebol3"

(Twitter API: statuses/user_timeline)

twitter/home

Returns a page of status updates for the current user’s friends:

twitter/home
twitter/home/page 2

(Twitter API: statuses/home_timeline)

twitter/update

Send a status update with optional reply reference:

twitter/update "Working on Rebol projects!"
twitter/update/reply "Agreed!" #10147840233

(Twitter API: statuses/update)

Examples

Print some status updates:

foreach tweet twitter/timeline/for "rebol3" [
print ""
print tweet/created_at
print tweet/text
]

Get a complete timeline for the current user (switch out the tags appropriately):

Rebol [
Title: "Twitter Export"
Date: 11-Sep-2010
]

do/args http://reb4.me/r/twitter context [
Consumer-Key: <your API key goes here>
Consumer-Secret: <your API secret goes here>
User-Store: <file-or-url to store users>
]

twitter/as <twitter user>

twitter-export: func [
for [string!] /timeout time [integer! time!]
/local result page content
][
result: copy []
page: 1

while [
content: twitter/timeline/for/page/size for page 100
not empty? content
][
append result content
page: page + 1
wait any [time 10] ; time between request, adjust as required
]

result
]

timeline: twitter-export ask "Export timeline for? "

See Also: