Archive

Posts Tagged ‘Twitter’

Build your Twitter API app in Clojure, Part 1

September 22nd, 2009

Building your own Twitter API application in Clojure is made real easy now thanks to clojure-twitter and clj-oauth libraries.

In this part, I will simply show you how easy it is to access the Twitter API using clojure-twitter. Here, I update my own Twitter status:

(require 'twitter ['oauth.client :as 'oauth])

(def consumer (oauth/make-consumer <consumer-token>
                                   <consumer-token-secret>
                                   "http://twitter.com/oauth/request_token"
                                   "http://twitter.com/oauth/access_token"
                                   "http://twitter.com/oauth/authorize"
                                   :hmac-sha1))

(def request-token (:oauth_token (oauth/request-token consumer)))

(oauth/user-approval-uri consumer
                         request-token
                         <callback-uri>)

(def access-token-response (oauth/access-token consumer
                                               request-token
                                               <verifier>))

(twitter/with-oauth consumer (:oauth_token access-token-response)
                             (:oauth_token_secret access-token-response)
  (twitter/update-status "#clojure is awesome"))


Making it all work:

  • Download the latest version of clojure-twitter
  • Download the latest version of clj-oauth
  • Add clojure-twitter.jar and dependents in the clojure-twitter/lib directory to your classpath
  • Next, you will need to register an application at Twitter here in order to obtain a Consumer token and token secret
  • Once you have register the application, launch REPL and replace <consumer-token> and <consumer-token-secret> in the above code snippet accordingly
  • Additionally, replace the <callback-uri> with the “Application Website” you defined in the application registration.
  • Once the user approval URI has been accessed and accepted, the verifier PIN code will be provide which needs to replace the <verifier>

In part 2, I will go through a more detailed, step-by-step example that demonstrates building a real-world Twitter API app in Clojure. Follow me on Twitter and I’ll keep you posted.

alen Clojure , ,