Created
June 14, 2023 18:29
-
-
Save apeckham/cd038dd45fb6b0af876285517f41ec22 to your computer and use it in GitHub Desktop.
invoke authenticated google cloud function from clojure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns yyyyyyyyyyaaaaaaaaaaa | |
(:import | |
(com.google.auth.oauth2 GoogleCredentials IdTokenCredentials)) | |
(:require | |
[clj-http.client :as client])) | |
(def root "https://bang.a.run.app") | |
(defn get-access-token | |
[] | |
(let [credentials (-> (IdTokenCredentials/newBuilder) | |
(.setIdTokenProvider (-> (GoogleCredentials/getApplicationDefault) | |
(.createScoped | |
["https://www.googleapis.com/auth/cloud-platform"]))) | |
(.setTargetAudience root) | |
.build)] | |
(.refresh credentials) | |
(.getTokenValue (.getIdToken credentials)))) | |
(defn invoke | |
[access-token path form-params] | |
(:body (client/post (str root path) | |
{:as :json | |
:content-type :json | |
:form-params form-params | |
:headers {"Authorization" (str "bearer " access-token)}}))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment