Clean up creds parsing a little

This commit is contained in:
2020-08-05 15:26:23 -05:00
parent 9dfc548a6f
commit df46647838

View File

@@ -17,16 +17,17 @@
[] []
(let [ (let [
jsonStr (slurp "./resources/tokens.json") jsonStr (slurp "./resources/tokens.json")
json (json/parse-string jsonStr true)
] ]
(json/parse-string jsonStr true))) json))
(defn exchange-auth-code-for-token (defn exchange-auth-code-for-token
"Gets and auth token from the auth code" "Gets and auth token from the auth code"
[auth_code] [auth_code creds]
(let [ (let [
url "https://oauth2.googleapis.com/token" url "https://oauth2.googleapis.com/token"
creds (parse-creds)
tokenResponse (client/post url { tokenResponse (client/post url {
:as :json
:form-params { :form-params {
:code auth_code :code auth_code
:grant_type "authorization_code" :grant_type "authorization_code"
@@ -36,14 +37,12 @@
:redirect_uri (first (:redirect_uris creds)) :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 (defn open-browser-for-auth
"Opens browser so user can give auth to use photos api", "Opens browser so user can give auth to use photos api",
[] [creds]
(def creds (parse-creds))
(def url (str (def url (str
(:auth_uri creds) "?" (:auth_uri creds) "?"
"redirect_uri=" (first (:redirect_uris creds)) "redirect_uri=" (first (:redirect_uris creds))
@@ -59,12 +58,13 @@
(defn get-new-token (defn get-new-token
"Goes through the full auth process to get a 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: ") (do (print "Paste your authorization code here: ")
(flush) (flush)
(def auth_code (read-line))) (def auth_code (read-line)))
(let [ (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 }) tokens-json (json/generate-string { :access_token access-token })
] ]
(spit "./resources/tokens.json" tokens-json) (spit "./resources/tokens.json" tokens-json)
@@ -81,8 +81,9 @@
[] []
(def token (authorize)) (def token (authorize))
(def r (client/get "https://photoslibrary.googleapis.com/v1/mediaItems" { (def r (client/get "https://photoslibrary.googleapis.com/v1/mediaItems" {
:as :json
:content-type :json :content-type :json
:headers { :headers {
:authorization (str "Bearer " token) :authorization (str "Bearer " token)
} })) } }))
(println r)) (println (:body r)))