drone29a / clj-oauth

OAuth Consumer support for Clojure
BSD 2-Clause "Simplified" License
273 stars 52 forks source link

How do you use credentials with http/get? #40

Closed drakezhard closed 9 years ago

drakezhard commented 9 years ago

Is this how you're supposed to setup a get request? (def credentials (oauth/credentials consumer
(:oauth_token access-token-response)
(:oauth_token_secret access-token-response)
:GET
"https://www.khanacademy.org/api/v1/user?email=default@example.org"))

(http/get "https://www.khanacademy.org/api/v1/user?email=default@example.org" :query-params credentials) => clojure.lang.Keyword cannot be cast to clojure.lang.IPersistentCollection

drone29a commented 9 years ago

If you're using clj-http, it looks like you should wrap the GET options in a map.

(http/get "https://www.khanacademy.org/api/v1/user?email=default@example.org" 
          {:query-params credentials})

I see the example in the README doesn't include that, perhaps it was a change to clj-http.

Please let me know if that helps.

drakezhard commented 9 years ago

I had already tried that, It doesn't work I get a 401 unauthorized response. I got a new API token from Khan Academy, see if you can diagnose my workflow and tell me where I went wrong, I'm really new to this sorry.

$ lein try clj-oauth clj-http
(def consumer-key "B9nyR6w6NbLRzbQr")
(def consumer-secret "pUQFGKGebZVhEuCA")
(def consumer (oauth/make-consumer consumer-key
                                   consumer-secret
                                   "https://www.khanacademy.org/api/auth/request_token"
                                   "https://www.khanacademy.org/api/auth/access_token"
                                   "https://www.khanacademy.org/api/auth/authorization"
                                   :hmac-sha1))

(def request-token (oauth/request-token consumer))
=> 
CompilerException java.lang.Exception: Got non-success code: 302. Content: 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
&lt;title>Redirecting...</title>
&lt;h1>Redirecting...</h1>
&lt;p>You should be redirected automatically to target URL: 
&lt;a href="/login/mobileoauth?oauth_map_id=4899792109699072&amp;view=normal&amp;register=0">/login/mobileoauth?oauth_map_id=4899792109699072&amp;view=normal&amp;register=0
&lt;/a>.  If not click the link., compiling:(form-init1852529498085389495.clj:1:20) ">/login /mobileoauth?oauth_map_id=4899792109699072&amp;view=normal&amp;register=0
&lt;/a>.  If not click the link., compiling:(form-init1852529498085389495.clj:1:20)?> 

I figure from the 302 code, that I'll have to create an http server and post the html in the exception, but I don't know how to do it I'm looking at Pedestal right now.

What I did was manually enter my credentials(username: tobyoauth@gmail.com password: examplepassword) at: https://www.khanacademy.org/login/mobileoauth?oauth_map_id=5869415504281600&amp;view=normal&amp;register=0

It redirected me to the default callback:

https://www.khanacademy.org/api/auth/default_callback?oauth_token_secret=BGxCGysbAXgGH87c&oauth_token=t6714632486780928

I then made a map defining the request:

(def request-token {:oauth_token "t6714632486780928"
                               :oauth_token_secret "BGxCGysbAXgGH87c"})

(def access-token-response (oauth/access-token consumer 
                                                   request-token))
(def credentials 
  (oauth/credentials consumer                                  
                     (:oauth_token access-token-response)
                     (:oauth_token_secret access-token-response)                              
                     :GET
                     "https://www.khanacademy.org/api/v1/user?email=tobyoauth@gmail.com"))

(http/get "https://www.khanacademy.org/api/v1/user?email=tobyoauth@gmail.com" 
          {:query-params credentials})
=> ExceptionInfo clj-http: status 401  clj-http.client/wrap-exceptions/fn--6955 (client.clj:171)
drakezhard commented 9 years ago

I got the problem already thanks. The thing is that 'oauth/credentials takes a base url and a map of query params.

(def credentials 
      (oauth/credentials consumer                                  
                         (:oauth_token access-token-response)
                         (:oauth_token_secret access-token-response)                              
                         :GET
                         "https://www.khanacademy.org/api/v1/user"
                         {:email "tobyoauth@gmail.com"}))

And you where right about 'http/get XD.

(http/get "https://www.khanacademy.org/api/v1/user?email=tobyoauth@gmail.com"
              {:query-params credentials})