https://github.com/udhos/groupcache_oauth2 implements the oauth2 client_credentials flow cacheing tokens with groupcache.
See full example: cmd/groupcache-oauth2-client-example/main.go.
import "github.com/udhos/groupcache_oauth2/clientcredentials"
// initialize groupcache and return workspace.
// see the example client for an implementation of this function.
groupcacheWorkspace := startGroupcache()
options := clientcredentials.Options{
TokenURL: tokenURL,
ClientID: clientID,
ClientSecret: clientSecret,
Scope: scope,
HTTPClient: http.DefaultClient,
GroupcacheWorkspace: groupcacheWorkspace,
}
client := clientcredentials.New(options)
// use client to make requests, it will automatically fetch and cache tokens.
// client.Do automatically manages token retrieval, caching, and background refreshing.
resp, errDo = client.Do(req)Test using this token server: https://oauth.tools/collection/1599045253169-GHF
go install github.com/udhos/oauth2/cmd/groupcache-oauth2-client-example@latest
groupcache-oauth2-client-example -tokenURL https://login-demo.curity.io/oauth/v2/oauth-token -clientID demo-backend-client -clientSecret MJlO3binatD9jk1git clone https://github.com/udhos/groupcache_oauth2
cd groupcache_oauth2
./build.sh