2

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.

LocustHorde
  • 6,361
  • 16
  • 65
  • 94
  • the single vs double quotes bit could matter. try replacing ' with \" in your header – Arthur Ulfeldt May 15 '15 at 16:41
  • 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: https://github.com/adamwynne/twitter-api - it makes use of clj-oauth internally too. – iamserious May 19 '15 at 16:16
  • @iamserious your suggested library finally worked, I could see the source to work out what I wanted but ended up using the library itself.. do you want to submit that as answer so I can accept it? – LocustHorde May 20 '15 at 18:55
  • 1
    Ha! Had I thought about the "I'm trying to post a twitter status update with clojure..." part this might have triggered the same answer:-) – pete23 May 22 '15 at 18:36

2 Answers2

3

It might be worth taking a look at Matt Revelle's OAuth client library for Clojure. https://github.com/mattrepl/clj-oauth

OAuth is sufficiently non-trivial to make it tiresome to hack together something from bare bones. If nothing else and you still want to go the minimal route, you might get some ideas.

pete23
  • 2,204
  • 23
  • 28
  • clj-oauth even has an example on its front page of how to access twitter. – mac May 16 '15 at 18:59
  • Hi Pete, I've tried using clj-oauth just now and like in the example on repo, I define a consumer, define the credentials and post with `(http/post url {:query-params credentials})` but I get a 403 `couldn't authenticate you` back from twitter. I noticed the example doesn't include my application keys so I added them in header for `POST` and that definitely doesn't work (`you don't have that permission` response). The credentials are passed in `:query-params` and that doesn't look right to me... any thoughts? – LocustHorde May 18 '15 at 20:33
  • can you try the Running Tests section of the clj-oauth README? – pete23 May 22 '15 at 18:34
2

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.

iamserious
  • 5,385
  • 12
  • 41
  • 60