API Reference¶
This page provides a breakdown of the starlette-discord OAuth2 API.
Discord OAuth2 Client¶
- class starlette_discord.DiscordOAuthClient(client_id, client_secret, redirect_uri, scopes=('identify',))¶
Client for Discord Oauth2.
- Parameters:
- await login(code)¶
Shorthand for session setup + identify.
- await login_return_token(code)¶
Shorthand for session setup + identify. Returns user and token.
- redirect(state=None, prompt=None, redirect_uri=None)¶
Returns a RedirectResponse that directs to Discord login.
- Parameters:
state (Optional[
str]) – Optional state parameter for Discord redirect URL. Docs can be found here.prompt (Optional[
str]) – Optional prompt parameter for Discord redirect URL. Ifconsent, user is prompted to re-approve authorization. Ifnone, skips authorization if user has already authorized. Defaults toconsent.redirect_uri (Optional[
str]) – Optional redirect URI to pass to Discord. Defaults to the client’s redirect URI.
- session(code) DiscordOAuthSession¶
Create a new DiscordOAuthSession from an authorization code.
- Parameters:
code (
str) – The OAuth2 code provided by the Discord API.- Returns:
A new OAuth session.
- Return type:
Discord OAuth2 Session¶
- class starlette_discord.DiscordOAuthSession(*args: Any, **kwargs: Any)¶
Session containing data for a single authorized user. Handles authorization internally.
Warning
It is recommended to not construct this class directly. Use DiscordOAuthClient.session or DiscordOAuthClient.session_from_token instead.
Note
Either the ‘code’ or ‘token’ parameter must be provided, but not both.
- Parameters:
code (Optional[
str]) – Authorization code included with user request after redirect from Discord.token (Optional[Dict[
str, Union[str,int, :class`float`]]]) – A previously generated, valid, access token to use instead of the OAuth code exchangeclient_id (
str) – Your Discord application client ID.scope (
str) – Discord authorization scopes separated by %20.redirect_uri (
str) – Your Discord application redirect URI.code – Authorization code included with user request after redirect from Discord.
token – A previously generated, valid, access token to use instead of the OAuth code exchange
- property cached_connections¶
The session’s cached account connections, if a connections() request has previously been made.
- Type:
List[
dict]
- property cached_guilds¶
The session’s cached guilds, if a guilds() request has previously been made.
- Type:
List[
dict]
- property cached_user¶
The session’s cached user, if an identify() request has previously been made.
- Type:
- await connections()¶
Fetch a user’s linked 3rd-party accounts.
- Returns:
The user’s connections.
- Return type:
List[
Connection]
- await identify()¶
Identify a user.
- Returns:
The user who authorized the application.
- Return type:
- await join_guild(guild_id, bot_token, user_id=None)¶
Add a user to a guild.
- new_state()¶
Generate a new state string for verifying authorizations.
- Returns:
The state string that was generated.
- Return type:
- await refresh_token(token_url, refresh_token=None, body='', auth=None, timeout=None, headers=None, verify_ssl=True, proxies=None, **kwargs)¶
Fetch a new access token using a refresh token. :param token_url: The token endpoint, must be HTTPS. :param refresh_token: The refresh_token to use. :param body: Optional application/x-www-form-urlencoded body to add the
include in the token request. Prefer kwargs over body.
- Parameters:
auth – An auth tuple or method as accepted by requests.
timeout – Timeout of the request in seconds.
headers – A dict of headers to be used by requests.
verify – Verify SSL certificate.
proxies – The proxies argument will be passed to requests.
kwargs – Extra parameters to include in the token request.
- Returns:
A token dict