From df46647838760e0245a6f82dd956164388feea98 Mon Sep 17 00:00:00 2001 From: benjaminramey Date: Wed, 5 Aug 2020 15:26:23 -0500 Subject: [PATCH] Clean up creds parsing a little --- src/google_photos_downloader/googleapi.clj | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/google_photos_downloader/googleapi.clj b/src/google_photos_downloader/googleapi.clj index c9567ea..69d8676 100644 --- a/src/google_photos_downloader/googleapi.clj +++ b/src/google_photos_downloader/googleapi.clj @@ -17,16 +17,17 @@ [] (let [ jsonStr (slurp "./resources/tokens.json") + json (json/parse-string jsonStr true) ] - (json/parse-string jsonStr true))) + json)) (defn exchange-auth-code-for-token "Gets and auth token from the auth code" - [auth_code] + [auth_code creds] (let [ url "https://oauth2.googleapis.com/token" - creds (parse-creds) tokenResponse (client/post url { + :as :json :form-params { :code auth_code :grant_type "authorization_code" @@ -36,14 +37,12 @@ :redirect_uri (first (:redirect_uris creds)) } }) - json (json/parse-string (:body tokenResponse) true) ] - (:access_token json))) + (:access_token (:body tokenResponse)))) (defn open-browser-for-auth "Opens browser so user can give auth to use photos api", - [] - (def creds (parse-creds)) + [creds] (def url (str (:auth_uri creds) "?" "redirect_uri=" (first (:redirect_uris creds)) @@ -59,12 +58,13 @@ (defn get-new-token "Goes through the full auth process to get a new token" [] - (open-browser-for-auth) + (def creds (parse-creds)) + (open-browser-for-auth creds) (do (print "Paste your authorization code here: ") (flush) (def auth_code (read-line))) (let [ - access-token (exchange-auth-code-for-token auth_code) + access-token (exchange-auth-code-for-token auth_code creds) tokens-json (json/generate-string { :access_token access-token }) ] (spit "./resources/tokens.json" tokens-json) @@ -81,8 +81,9 @@ [] (def token (authorize)) (def r (client/get "https://photoslibrary.googleapis.com/v1/mediaItems" { + :as :json :content-type :json :headers { :authorization (str "Bearer " token) } })) - (println r)) \ No newline at end of file + (println (:body r))) \ No newline at end of file