gd6d3q
Last Updated: February 25, 2016
·
1.193K
· yehohanan7

Clojure - dropbox integration

Getting the Access token and access secret

(defn user-authorized? []
 (loop [_ (println "Have you authorized the app to access your data?")
     user-input (read-line)]
(if (= user-input "yes")
  'true
  (recur nil read-line))
)
  )

(defn open-session [consumer-key consumer-secret]
(WebAuthSession. (AppKeyPair. consumer-key consumer-secret)     (Session$AccessType/DROPBOX))
 )

(defn get-access-keys [user-name]
(let
[session (open-session consumerkey consumersecret)
 auth-info (.getAuthInfo session)
 ]
(println "access this link and provide permission:  " (.url auth-info))
(when (user-authorized?)
  (. session (retrieveWebAccessToken (.requestTokenPair auth-info)))
  (let [key-pairs (. session (getAccessTokenPair))]
    [(.key key-pairs) (.secret key-pairs)]
    )
  )
  )
  )

(defn get-access [user-name]
(let [[access-token access-key] (get-access-keys user-name)]
{:id user-name :access-token access-token :access-secret access-key}
)
)

Fetcthing users stats from dropbox

(require '(com.cloud [oauth :as oauth]))
(defn get-stats [user]
(oauth/http-get "https://api.dropbox.com/1/account/info" (:access-token  user) (:access-secret user) {})
 )

oauth file

(ns com.cloud.oauth)

(import '(javax.crypto.spec SecretKeySpec))
(import '(oauth.signpost.commonshttp CommonsHttpOAuthConsumer ))
(import '(oauth.signpost.basic DefaultOAuthConsumer))
(import '(org.apache.http.client.methods HttpGet HttpPost))
(import '(java.net HttpURLConnection URL))
(import '(org.apache.http HttpRequest))
(import '(org.apache.http.impl.client DefaultHttpClient BasicResponseHandler))

(import '(javax.xml.crypto.dsig SignatureMethod))

(defn http-get [url token secret params]
(let [consumer (CommonsHttpOAuthConsumer. consumerkey  consumersecret) http-client (new DefaultHttpClient) get-request (new  HttpGet url)]
(. consumer setTokenWithSecret token secret)
(. consumer sign get-request)
(. http-client execute get-request (new BasicResponseHandler))
)
)