From 9dfc548a6f9184e61cfb41be37c754ee03bab7a0 Mon Sep 17 00:00:00 2001 From: benjaminramey Date: Wed, 5 Aug 2020 15:21:54 -0500 Subject: [PATCH] Actually gets media items now! --- project.clj | 3 +- resources/tokens.json | 1 + src/google_photos_downloader/core.clj | 5 +- src/google_photos_downloader/googleapi.clj | 89 +++++++++++++++++++++- 4 files changed, 91 insertions(+), 7 deletions(-) create mode 100644 resources/tokens.json diff --git a/project.clj b/project.clj index 36773bf..b48236f 100644 --- a/project.clj +++ b/project.clj @@ -4,7 +4,8 @@ :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/"} :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 :target-path "target/%s" :profiles {:uberjar {:aot :all diff --git a/resources/tokens.json b/resources/tokens.json new file mode 100644 index 0000000..4ecd796 --- /dev/null +++ b/resources/tokens.json @@ -0,0 +1 @@ +{"access_token":"ya29.a0AfH6SMASYFOrfM4heTJMx7PotFukUejvS-16-X9vd3_gDwNiNHkxBPHAwyGGkBxYbglo7agPOk1Xp6fLF65VkrxNoGS0MCXYFACR7hucGbWAUemlrK4LHe42iFm1RdmDXpvRvJimBiDm3GqTLSftTiDcj_ioyqrXnC0"} \ No newline at end of file diff --git a/src/google_photos_downloader/core.clj b/src/google_photos_downloader/core.clj index 3c75955..c383c63 100644 --- a/src/google_photos_downloader/core.clj +++ b/src/google_photos_downloader/core.clj @@ -1,8 +1,9 @@ (ns google-photos-downloader.core (:gen-class) - (:require [clj-http.client :as client])) + (:require [clj-http.client :as client]) + (:require [google-photos-downloader.googleapi :as googleapi])) (defn -main "I don't do a whole lot ... yet." [& args] - (print (:body (client/get "https://www.benramey.com")))) + (println (googleapi/get-media-items))) diff --git a/src/google_photos_downloader/googleapi.clj b/src/google_photos_downloader/googleapi.clj index 4560348..c9567ea 100644 --- a/src/google_photos_downloader/googleapi.clj +++ b/src/google_photos_downloader/googleapi.clj @@ -1,7 +1,88 @@ (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 - "Gets a token from the Google OAuth" +(defn parse-creds + "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", [] - ()) \ No newline at end of file + (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)) \ No newline at end of file