Groovy for Domain-specific Languages - Second Edition

(nextflipdebug2) #1
Chapter 6

[ 115 ]

Twitter APIs are all pure HTTP-based requests. Any method that just retrieves data
such as a search operation is implemented by using an HTTP GET request, whereas
any method that updates, deletes, or creates, an object is implemented by using
an HTTP POST request. Most of the APIs conform to REST design principals and
support XML and JSON data formats, RSS, and Atom Syndication Formats.


We can interact with the Twitter APIs by using any tool or programming
language that allows us to interact with a web server using HTTP
requests.
In order to use the APIs, you first need to create an application with your
own Twitter user account. To do this, you will need to log in to Twitter at
http://apps.twitter.com. Click on the button to Create New App,
then complete the form to create your app. Next, click on the Permissions
tab and change the Access type to Read, Write and direct messages.
Finally, navigate to the Keys and Access Tokens page, and click on the
Create my access token button.
You now have four unique codes generated for this account. To run the
examples, you will need to copy the generated keys and secrets into the
twitter4j.properties file in the examples directories.
Before we go on, you can test out the raw APIs using the Twitter online
OAuth tool. Just click on the Test OAuth button on the App page. Try a
simple search via the APIs by typing the following into the request URI:
https://api.twitter.com/1.1/search/tweets.
json?q=Groovy
This will generate the OAuth signature headers for you so that you can
try the API out.

Try out the generated cURL command at the command line for example (your
version will have keys and secrets that are generated for your account):


curl --get 'https://api.twitter.com/1.1/search/tweets.json'
--data 'q=Groovy' --header 'Authorization: OAuth oauthconsumer
key="xxxxxxxxxxxxxxxxxxxxxxxx", oauth_nonce="xxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxx", oauth_signature="xxxxxxxxxxxxxxxxxxxxx", oauthsignature
method="HMAC-SHA1", oauth_timestamp="1416572721", oauth_token="xxxxxxx-
xxxxxxxxxxxxxxxxxxxxxxx ", oauth_version="1.0"' --verbose


This will invoke the search API, searching for tweets containing the keyword
Groovy. The resulting list of tweets is formatted as JSON. cURL is a command-line
tool for fetching files using URL syntax. It's available by default on Mac OS X and
most Linux distributions, or can be downloaded from http://curl.haxx.se/
download.html. There is extensive documentation of the Twitter APIs that can be
found at https://dev.twitter.com/rest/public.

Free download pdf