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 [
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))
(println (:body r)))