Search code examples
oauthclojuretwitter-oauthclj-http

How to wrap oAuth headers in clj-http?


I'm trying to post a twitter status update with clojure... but this probably really is a question about oAuth headers, and using it through the wonderful clj-http library.

I've used clj-http before for basic-auth and other type of headers and it was fairly straightforward. The Authorization: Bearer ... was a bit confusing but I got there in the end.

For twitter though, I am supposed to pass a lot of variables in Authorization and I am not quite sure how I'd go about doing that. Here's the curl command, according to twitter, that I'll need to post the tweet:

curl --request 'POST' 'https://api.twitter.com/1.1/statuses/update.json' 
     --data 'status=this+is+sparta' 
     --header 'Authorization: OAuth oauth_consumer_key="xxxxx",
                              oauth_nonce="xxxxx", 
                              oauth_signature="xxxxx", 
                              oauth_token="xxxxx", oauth_version="1.0"' 
     --verbose

So I am not sure how I would append all the oAuth... things in the header. trying with (str ..) isn't really working out. Here's what I've got so far:

(client/post "https://api.twitter.com/1.1/statuses/update.json")
             {:headers {:Authorization (str "OAuth oauth_consumer_key='xxxxx', oauth_nonce='xxxxx', oauth_signature='xxxxx', oauth_token='xxxxx', oauth_version='1.0'" )}
              :form-params {:status "This is sparta"})

This returns 403. permission error when I try.

Any ideas on how I'd construct that query?

ps: I do have the oAuth token and token_secret for the account... but I notice the token_secret value isn't being passed? and what does oauth_nonce for? I'm for now passing the value that twitter gave me for curl... looking around it seems that it is just a random value so not that fussed about it.


Solution

  • I've had difficulty in the past trying to use clj-oauth (mostly because of my own understanding of clojure) but I found twitter-api pretty straightforward to use. It makes use of clj-oauth internally.