Skip to content

Instantly share code, notes, and snippets.

@cheikhsimsol
Last active February 7, 2024 23:12
Show Gist options
  • Save cheikhsimsol/e86609d8f7bc90a191b74d3591b5bf68 to your computer and use it in GitHub Desktop.
Save cheikhsimsol/e86609d8f7bc90a191b74d3591b5bf68 to your computer and use it in GitHub Desktop.
mux.HandleFunc("GET /consume", func(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
code := r.FormValue("code")
state := r.FormValue("state")
if state != "state" {
http.Error(w, "Error, mismatched state", http.StatusBadRequest)
return
}
tok, err := conf.Exchange(ctx, code)
if err != nil {
http.Error(w, "Error exchanging token", http.StatusInternalServerError)
return
}
client := conf.Client(ctx, tok)
res, err := client.Get("https://oauth.wiremockapi.cloud/userinfo")
if err != nil {
http.Error(w, "Error fetching data", http.StatusInternalServerError)
return
}
defer res.Body.Close()
// Set the content type based on the response header
w.Header().Set("Content-Type", res.Header.Get("Content-Type"))
// Copy the response body to the client's response writer
_, err = io.Copy(w, res.Body)
if err != nil {
http.Error(w, "Error streaming data", http.StatusInternalServerError)
return
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment