Actually gets media items now!

This commit is contained in:
2020-08-05 15:21:54 -05:00
parent 22e0ff0998
commit 9dfc548a6f
4 changed files with 91 additions and 7 deletions

View File

@@ -4,7 +4,8 @@
:license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0" :license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
:url "https://www.eclipse.org/legal/epl-2.0/"} :url "https://www.eclipse.org/legal/epl-2.0/"}
:dependencies [[org.clojure/clojure "1.10.1"] :dependencies [[org.clojure/clojure "1.10.1"]
[clj-http "3.10.1"]] [clj-http "3.10.1"]
[cheshire "5.10.0"]]
:main ^:skip-aot google-photos-downloader.core :main ^:skip-aot google-photos-downloader.core
:target-path "target/%s" :target-path "target/%s"
:profiles {:uberjar {:aot :all :profiles {:uberjar {:aot :all

1
resources/tokens.json Normal file
View File

@@ -0,0 +1 @@
{"access_token":"ya29.a0AfH6SMASYFOrfM4heTJMx7PotFukUejvS-16-X9vd3_gDwNiNHkxBPHAwyGGkBxYbglo7agPOk1Xp6fLF65VkrxNoGS0MCXYFACR7hucGbWAUemlrK4LHe42iFm1RdmDXpvRvJimBiDm3GqTLSftTiDcj_ioyqrXnC0"}

View File

@@ -1,8 +1,9 @@
(ns google-photos-downloader.core (ns google-photos-downloader.core
(:gen-class) (:gen-class)
(:require [clj-http.client :as client])) (:require [clj-http.client :as client])
(:require [google-photos-downloader.googleapi :as googleapi]))
(defn -main (defn -main
"I don't do a whole lot ... yet." "I don't do a whole lot ... yet."
[& args] [& args]
(print (:body (client/get "https://www.benramey.com")))) (println (googleapi/get-media-items)))

View File

@@ -1,7 +1,88 @@
(ns google-photos-downloader.googleapi (ns google-photos-downloader.googleapi
(:require [clj-http.client :as client])) (:require [clj-http.client :as client])
(:require [cheshire.core :as json])
(:require [clojure.java.browse :as browse]))
(defn getAuthToken (defn parse-creds
"Gets a token from the Google OAuth" "Parse credentials from credentials.json file"
[]
(let [
jsonStr (slurp "./resources/credentials.json")
json (json/parse-string jsonStr true)
]
(:installed json)))
(defn parse-tokens
"Parses the tokens.json file"
[]
(let [
jsonStr (slurp "./resources/tokens.json")
]
(json/parse-string jsonStr true)))
(defn exchange-auth-code-for-token
"Gets and auth token from the auth code"
[auth_code]
(let [
url "https://oauth2.googleapis.com/token"
creds (parse-creds)
tokenResponse (client/post url {
:form-params {
:code auth_code
:grant_type "authorization_code"
:scope ""
:client_id (:client_id creds)
:client_secret (:client_secret creds)
:redirect_uri (first (:redirect_uris creds))
}
})
json (json/parse-string (:body tokenResponse) true)
]
(:access_token json)))
(defn open-browser-for-auth
"Opens browser so user can give auth to use photos api",
[] []
()) (def creds (parse-creds))
(def url (str
(:auth_uri creds) "?"
"redirect_uri=" (first (:redirect_uris creds))
"&auto_refresh_url=" (:token_uri creds)
"&prompt=select_account"
"&access_type=offline"
"&response_type=code"
"&client_id=" (:client_id creds)
"&scope=https://www.googleapis.com/auth/photoslibrary.readonly"
))
(browse/browse-url url))
(defn get-new-token
"Goes through the full auth process to get a new token"
[]
(open-browser-for-auth)
(do (print "Paste your authorization code here: ")
(flush)
(def auth_code (read-line)))
(let [
access-token (exchange-auth-code-for-token auth_code)
tokens-json (json/generate-string { :access_token access-token })
]
(spit "./resources/tokens.json" tokens-json)
(access-token)))
(defn authorize
"Gets an existing auth token (saved previously) or runs user through auth process"
[]
(let [existing-token (:access_token (parse-tokens))]
(if (nil? existing-token) (get-new-token) existing-token)))
(defn get-media-items
"Gets media items of user"
[]
(def token (authorize))
(def r (client/get "https://photoslibrary.googleapis.com/v1/mediaItems" {
:content-type :json
:headers {
:authorization (str "Bearer " token)
} }))
(println r))