Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OAuth1 in Clojure

I'm trying to integrate with an API (Context.IO) using Clojure. Context.IO uses OAuth 1, which requires to inform consumer key and consumer secret credentials to integrate with.

I've manage to work with Context.IO with Node.JS in the past, using request library (https://github.com/request/request). It turn out to be quite simple, just filled consumer_key and consumer_secret in an object and passed it in oauth parameter in the request.

var oauth   = 
{
  consumer_key: 'dsfdfssdf',
  consumer_secret: 'dasfsafdsf'
};

request.post( { url:url, oauth:oauth } )

Now I'm trying to accomplish the same using clj-oauth https://github.com/mattrepl/clj-oauth, but I'm kinda lost, because it requires too different parameters (for more complex use cases I guess), and I'm having a hard time trying to figure out how to do the simple.

To add more information, Context IO uses OAuth only for API Authentication, not user Authorization. So it doesn't require tokens to be informed, neither provides one. It only requires the consumer key and signature (the same described here: dev.twitter.com/oauth/overview/creating-signatures).

Can someone give an example similar to what I accomplished in Node using Clojure or clj-oauth (or any other library)? I haven't found a way to do so.

Thanks!

like image 765
Ricardo Mayerhofer Avatar asked Aug 20 '15 00:08

Ricardo Mayerhofer


1 Answers

I signed up for context io to give this a go. First, in leiningen I set up

:dependencies [[org.clojure/clojure "1.6.0"]
                 [clj-oauth "1.5.2"]
                 [clj-http "1.1.2"]] 

as my dependencies. There are two examples below. One calls a url without any parameters, the other calls the same url, but with parameters.

(ns scratch.core
  (:require [oauth.client :as oauth]
            [clj-http.client :as http]))

(def okey "key")

(def osecret "secret")

(def consumer (oauth/make-consumer okey
                                   osecret
                                   nil
                                   nil
                                   nil
                                   :hmac-sha1))

(defn test-get []
  (let [credentials (oauth/credentials consumer
                                       nil
                                       nil
                                       :GET
                                       "https://api.context.io/lite/users")]
    (http/get "https://api.context.io/lite/users" {:query-params credentials})))

(defn test-get-params []
  (let [params {:email "[email protected]"}
        credentials (oauth/credentials consumer
                                       nil
                                       nil
                                       :GET
                                       "https://api.context.io/lite/users"
                                       params)]
    (http/get "https://api.context.io/lite/users" {:query-params (merge credentials params)})))
like image 107
RedDeckWins Avatar answered Sep 20 '22 10:09

RedDeckWins