This is a starter repository from where a beginner can take inspiration to add OAuth2 system in their Golang application. This project doesn't use any 3rd party package. For OAuth2 flow, the official golang.org/x/oauth2
package is used.
- When I was learning, I didn't find a good source and code example to impliment OAuth2 in Golang.
- There are very easy to use package like Goth, but I don't want to add an additional 3rd party dependency.
- Token refresh and building HTTP client using the access token is not clear in the documentation.
-
Clone this repository
git clone https://github.com/hrishiksh/golang-oauth2-starter.git
-
Download the requirements
go mod download go mod tidy
-
Get Client ID and Client Secret from the Oauth provider. I am using Google for example. Go to the Google cloud credential page for getting client id and secret.
-
Add the Client ID and secret as an environment variable and run the application
CLIENT_ID="clientid" CLIENT_SEC="secret" go run .
- Don't add your logo in Google OAuth consent screen. Otherwise you have to varify your app from Google.