Last Updated: February 25, 2016
· 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")
  (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]
[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 '( [oauth :as oauth]))
(defn get-stats [user]
(oauth/http-get "" (:access-token  user) (:access-secret user) {})

oauth file


(import '(javax.crypto.spec SecretKeySpec))
(import '(oauth.signpost.commonshttp CommonsHttpOAuthConsumer ))
(import '(oauth.signpost.basic DefaultOAuthConsumer))
(import '(org.apache.http.client.methods HttpGet HttpPost))
(import '( 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))