DisGo includes a complete OAuth2 client for implementing Discord’s OAuth2 authorization flow. This allows you to authenticate users, access their Discord data, and manage access tokens.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/disgoorg/disgo/llms.txt
Use this file to discover all available pages before exploring further.
Creating an OAuth2 client
The OAuth2 client requires your application’s client ID and secret:Authorization flow
The OAuth2 authorization flow consists of three main steps:Generate authorization URL
Generate a URL to redirect users to Discord’s authorization page:The state parameter is automatically generated for CSRF protection.
Handle callback
When the user authorizes your app, Discord redirects them to your callback URL with a code and state:
Available OAuth2 scopes
You can request different scopes to access various user data:OAuth2ScopeIdentify- Basic user info (username, avatar, etc.)OAuth2ScopeEmail- User’s email addressOAuth2ScopeGuilds- List of guilds the user is inOAuth2ScopeGuildsMembersRead- Guild member infoOAuth2ScopeConnections- User’s connected accountsOAuth2ScopeRoleConnectionsWrite- Manage role connectionsOAuth2ScopeWebhookIncoming- Create incoming webhooks
Session management
Session structure
A session contains the access token and metadata:Refreshing tokens
Access tokens expire after a period (typically 7 days). Refresh them before expiration:Automatic verification
Verify and refresh if needed in one call:VerifySession checks if the session is expired and automatically refreshes it if necessary.Accessing user data
Once you have a valid session, you can access various user data based on the granted scopes.Get user information
Requires theidentify scope:
Get user guilds
Requires theguilds scope:
Get user connections
Requires theconnections scope:
Get guild member info
Requires theguilds.members.read scope:
State controller
The state controller manages CSRF tokens for the OAuth2 flow. DisGo provides a default implementation, but you can customize it.Custom state controller
Configure default state controller
Guild installation
You can prompt users to add your bot to a guild during OAuth2:When using
OAuth2ScopeBot, the user will be prompted to add your bot with the specified permissions.Complete web server example
Here’s a complete example implementing OAuth2 in a web server:Configuration options
WithLogger(*slog.Logger)- Set custom loggerWithRestClient(rest.Client)- Use custom REST clientWithRestClientConfigOpts(...rest.ClientConfigOpt)- Configure REST clientWithOAuth2(rest.OAuth2)- Custom OAuth2 REST implementationWithStateController(StateController)- Custom state controllerWithStateControllerOpts(...StateControllerConfigOpt)- Configure state controller
Best practices
Store sessions securely
Store sessions securely
Never store sessions in cookies or client-side storage. Keep them server-side in a secure database:
Validate state parameters
Validate state parameters
Always validate the state parameter to prevent CSRF attacks. The built-in state controller does this automatically:
Handle token expiration
Handle token expiration
Check for expiration before making API calls and refresh when needed:
Request minimal scopes
Request minimal scopes
Only request the OAuth2 scopes your application actually needs. Users are more likely to authorize apps that request fewer permissions.