API Reference

golang.org/x/oauth2

The Go OAuth2 client package providing token sources, configs, and HTTP client integration.

Official Documentation ↗
CompatibilityThis documentation covers version v0.20. Check the official docs for the latest API surface.

Installation

gogo get golang.org/x/oauth2

Methods

x/oauth2 handles authorization code and client credentials flows and injects tokens into requests.

Key Features

  • Auth code and client credentials flows
  • Automatic token refresh sources
  • HTTP client that attaches tokens

Usage Notes

Pin a version and check the official docs for the API surface you depend on before upgrading.

Errors

The following points are quoted verbatim from the official documentation. 来源:https://pkg.go.dev/golang.org/x/oauth2 · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:51:18.078Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.

Security notes

Quoted verbatim from the official documentation; this is not a complete threat model. 来源:https://pkg.go.dev/golang.org/x/oauth2 · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:51:18.078Z.

FAQ

How is the token refreshed?
The client returned by Config.Client uses a TokenSource that refreshes automatically before expiry.
What is the state parameter for?
It prevents CSRF in the redirect flow. Generate a random value, store it, and verify it on callback.
How do I request offline access?
Pass oauth2.AccessTypeOffline so a refresh token is issued, then persist it securely.