xml - How to configure OAuth with Goodreads in Clojure -
i trying configure oauth use access data goodreads api using clojure , compojure framework. want list of firends goodreads member has. should mention new clojure , new oauth , in general api authentication appreciate if explained possible.
i tried find answer online unable find 1 me many sites deal specific oauth api configurations , seem differ api api in terms of configuring access oauth. furthermore not many of them use clojure explain oauth api configuration , differ in terms of required specific api access. goodreads api page not helpful either. found answer this question , tried use template configure oauth access goodreads api. uses clj-oauth tried use in combination answer above mentioned question since replacing data answer question goodreads addresses didn't work. have returns 401 error , have no idea doing wrong:
(def okey "goodreads-key") (def osecret "goodreads-secret") (def consumer (oauth/make-consumer okey osecret "http://www.goodreads.com/oauth/request_token" "http://www.goodreads.com/oauth/access_token" "http://www.goodreads.com/oauth/authorize" :hmac-sha1)) (def request-token (oauth/request-token consumer nil)) (oauth/user-approval-uri consumer (:oauth_token request-token)) (def access-token-response (oauth/access-token consumer request-token nil)) (defn test-get [] (let [credentials (oauth/credentials consumer (:oauth_token access-token-response) (:oauth_token_secret access-token-response) :get "https://www.goodreads.com/friend/user/user-id-number?format=xml")] (http/get "https://www.goodreads.com/friend/user/user-id-number?format=xml" {:query-params credentials})))
the clj-oauth says in client example there should callback-uri in request-token , verifier in access-token-response, since not familiar oauth put nil there.
the answer above question doesn't use following lines code:
(def request-token (oauth/request-token consumer nil)) (oauth/user-approval-uri consumer (:oauth_token request-token)) (def access-token-response (oauth/access-token consumer request-token nil))
and subsequently doesn't use "http://www.goodreads.com/oauth/request_token" "http://www.goodreads.com/oauth/access_token" "http://www.goodreads.com/oauth/authorize" addresses in consumer
nor use (:oauth_token access-token-response) (:oauth_token_secret access-token-response) in test-get
, instead replaces values nil
, since didn't work tried combine example mentioned on clj-oauth github page mentioned above, still doesn't work.
i trying xml response of member's list of friends goodreads api , configuring appreciated.
so project.clj looks this:
:dependencies [[org.clojure/clojure "1.6.0"] [clj-oauth "1.5.2"] [clj-http "1.1.2"]]
sample code:
(ns scratch.core (:require [oauth.client :as oauth] [clj-http.client :as http])) (def consumer-key "your_key") (def consumer-secret "your_secret") ;step 1: create oath consumer object (def consumer (oauth/make-consumer consumer-key consumer-secret "https://www.goodreads.com/oauth/request_token" "https://www.goodreads.com/oauth/access_token" "https://www.goodreads.com/oauth/authorize" :hmac-sha1)) ;step 2: create request token. these can used once. expire well. (def request-token (oauth/request-token consumer nil)) ;step 3: have goodreads user approve application. can approve application visting url ; produced call below. (oauth/user-approval-uri consumer (:oauth_token request-token)) ;step 4: once goodreads user has approved application, can exchange request token access token. ;an access token per-user. goodreads in particular issue 1 per user, don not lose access token. (def access-token-response (oauth/access-token consumer request-token nil)) ;step 5: user id of user approved application. ;you can parse user id out this. used clojure.zip, clojure.data.zip.xml, , clojure.data.xml parse out. (defn get-user-id [] (let [url "https://www.goodreads.com/api/auth_user" credentials (oauth/credentials consumer (:oauth_token access-token-response) (:oauth_token_secret access-token-response) :get url)] (http/get url {:query-params credentials}))) (get-user-id) ;step 6: friends of user. (defn get-friends [user-id] (let [url (str "https://www.goodreads.com/friend/user/" user-id) params {:format "xml"} credentials (oauth/credentials consumer (:oauth_token access-token-response) (:oauth_token_secret access-token-response) :get url params)] (http/get url {:query-params (merge credentials params)}))) (get-friends <a user id>)
edit:
ok reran exact code above (redacted api keys , user id) , 100% sure works. highly recommend creating throwaway goodreads account test with.
Comments
Post a Comment