golang.org/x/oauth2
The Go OAuth2 client package providing token sources, configs, and HTTP client integration.
Official Documentation ↗v0.20. Check the official docs for the latest API surface.Installation
go get golang.org/x/oauth2Methods
No methods match this filter.
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.
- AuthCodeOption ) (* Token , error ) DeviceAccessToken polls the server to exchange a device code for a token. (https://pkg.go.dev/golang.org/x/oauth2)
- AuthCodeOption ) (* DeviceAuthResponse , error ) DeviceAuth returns a device auth struct which contains a device code and authorization information provided for users to enter on another device. (https://pkg.go.dev/golang.org/x/oauth2)
- AuthCodeOption ) (* Token , error ) Exchange converts an authorization code into a token. (https://pkg.go.dev/golang.org/x/oauth2)
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.
- Learn more about best practices Repository cs.opensource.google/go/x/oauth2 Links Report a Vulnerability Open Source Insights README ¶ OAuth2 for Go oauth2 package contains a client implementation for OAuth 2.0 spec. (https://pkg.go.dev/golang.org/x/oauth2)
- To protect against CSRF attacks, opts should include a PKCE challenge (S256ChallengeOption). (https://pkg.go.dev/golang.org/x/oauth2)
- Before calling Exchange, be sure to validate http.Request.FormValue ("state") if you are using it to protect against CSRF attacks. (https://pkg.go.dev/golang.org/x/oauth2)
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.
Report a problem with this API docOpens a pre-filled issue on GitHub.