Skip to content

Instantly share code, notes, and snippets.

@apeckham
Created June 14, 2023 18:29
Show Gist options
  • Save apeckham/cd038dd45fb6b0af876285517f41ec22 to your computer and use it in GitHub Desktop.
Save apeckham/cd038dd45fb6b0af876285517f41ec22 to your computer and use it in GitHub Desktop.
invoke authenticated google cloud function from clojure
(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