Go package for OAuth2 desktop authorization flows. Handles the browser consent + local callback server pattern used by CLI tools to acquire initial OAuth2 tokens.
import "github.com/LeGambiArt/oauth2flow"
tok, err := oauth2flow.Run(ctx, oauth2flow.Config{
ClientCredentialsFile: "client-credentials.json",
TokenFile: "token.json",
Scopes: []string{"https://www.googleapis.com/auth/calendar"},
})- Loads client ID/secret from the credentials JSON file
- Starts a local HTTP server for the OAuth2 callback
- Opens the browser to the consent URL
- Waits for the authorization code via callback
- Exchanges the code for a token
- Saves the token to disk with 0600 permissions
Tokens are saved as JSON compatible with Google's token format:
{
"access_token": "...",
"token_type": "Bearer",
"refresh_token": "...",
"expiry": "2026-01-01T00:00:00Z"
}- Linux (
xdg-open) - macOS (
open)
GPLv3 - see LICENSE.