Last active
October 22, 2024 20:29
-
-
Save youcefguichi/f04c75fb9183b4137f1602e5e9b05a94 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Example of handling Oauth Callback using Golang. | |
// this function is serving at this endpoint | |
// http://localhost:3005/oauth2/callback | |
func OAuthCallback(w http.ResponseWriter, r *http.Request) { | |
// this is getting the code value | |
// from our callback url http://localhost:3005/oauth2/callback?code=<some-code-here> | |
code := r.URL.Query().Get("code") | |
// We are exchanging the code for an access_token. | |
// the Exchange function is simply creating a post request to https://github.com/login/oauth/access_token | |
// with this follwoing payload: | |
// - code: the code that we got earlier. | |
// - client_id | |
// - client_secret | |
// if we use curl it will look like this: | |
// curl -X POST https://github.com/login/oauth/access_token \ | |
// -H "Content-Type: application/json" \ | |
// -H "Accept: application/json" \ | |
// -d '{ | |
// "client_id": "the_app_client_id", | |
// "client_secret": "the_app_client_secret", | |
// "code": "the_code_you_received", | |
// }' | |
access_token, err := oauth2.Config.Exchange(r.Context(), code) | |
if err != nil { | |
Logger.Error("failed oauth exchange", "error", err) | |
w.WriteHeader(http.StatusInternalServerError) | |
return | |
} | |
} | |
// by www.crashloop.sh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment