Go applications
Go programming language (Golang) provides a package golang.org/x/oauth2 to implement the OAuth2.0 protocol.
Make OAuth 2.0 configuration
The first step is to define a configuration. A reference to downloaded credentials properties is used in all code examples.
conf := &oauth2.Config{
ClientID: <Application ClientID>,
ClientSecret: <Application Client-Secret>,
Scopes: []string{
"openid profile",
},
Endpoint: oauth2.Endpoint{
AuthURL: <pu> + <oa>,
TokenURL: <pu> + <ot>,
},
}
Obtain tokens
Now you are ready to obtain tokens. Token struct in Go contains both access and refresh tokens.
tok, err := conf.PasswordCredentialsToken(oauth2.NoContext, <Service Account AccessKey>, <Service Account SecretKey>)
if err != nil {
// handle error
}
Create HTTP client and make a request
The OAuth 2.0 configuration struct also has a method to create the HTTP client.
client := conf.Client(oauth2.NoContext, tok)
resp, err := client.Get(<Request URL>)
if err != nil {
// handle error
}
Note: You do not need to refresh the
token manually; the client does it automatically.
Revoke tokens
The package does not provide methods to revoke any token. You can call the revoke service directly.
resp, err := http.Get(<pu> + <or> + "?token=" + tok.AccessToken)
if err != nil {
// handle error
}