Client

class discordoauth2.Client(id, secret, redirect, bot_token=None)
Parameters:
  • id (int) – The application ID

  • secret (str) – The applications secret, this should be secret!

  • redirect (str) – The redirect URL for oauth2

  • bot_token (str) – When adding a user to a guild, a bot with sufficent permissions is required. if you’re not going to add members to a guild leave this empty.

id

The application’s ID

Type:

int

redirect_url

The redirect URL

Type:

str

from_access_token(access_token)

Creates a discordoauth2.PartialAccessToken object from a code. This is useful so you can store the PartialAccessToken.token and then continue using it.

Parameters:

access_token (str) – The code from oauth2, it is the code paramater on successful return redirect urls from discord’s oauth2.

Returns:

discordoauth2.PartialAccessToken

exchange_code(code)

Converts a code from the redirect url into a discordoauth2.AccessToken

Parameters:

code (str) – code paramater from OAuth2 redirect URL

Returns:

discordoauth2.AccessToken

Raises:
  • discordoauth2.exceptions.HTTPException – The request failed, usally because the client ID, client, secret, redirect or code is incorrect

  • discordoauth2.exceptions.RateLimited – You’re being rate limited.

refresh_token(refresh_token)

Converts a refresh token into a new discordoauth2.AccessToken. You should store the refresh token and access token, so you can renew the access token when it expires.

Parameters:

refresh_token (str) – The refresh token, can be found from discordoauth2.AccessToken.refresh_token

Returns:

discordoauth2.AccessToken

Raises:
  • discordoauth2.exceptions.HTTPException – The request failed, usally because the client ID, client, secret, redirect or code is incorrect

  • discordoauth2.exceptions.RateLimited – You’re being rate limited.

client_credentails_grant(scope)

Creates an discordoauth2.AccessToken on behalf of the application’s owner.

Parameters:

scope (list[str]) – List of scopes.

Returns:

discordoauth2.AccessToken

Raises:
  • discordoauth2.exceptions.HTTPException – The request failed, usally because the client ID, client, secret, redirect or code is incorrect

  • discordoauth2.exceptions.RateLimited – You’re being rate limited.

Warning

If the application is owned by a team, you can only request for the identify scope. You can also request applications.commands.update, but the library does not support it yet.``

revoke_token(token, token_type=None)

Revokes an OAuth2 token related to the client.

New in version 1.1.

Parameters:
  • token (str) – Access/Refresh token to revoke

  • token_type (Optional[str]) – Not required, but should be either refresh_token or access_token

Raises:
  • discordoauth2.exceptions.HTTPException – The request failed, usally because the client ID, client, secret, redirect or code is incorrect

  • discordoauth2.exceptions.RateLimited – You’re being rate limited.

update_linked_roles_metadata(metadata)

Updates the application’s linked roles metadata, requires bot token to have been provided.

New in version 1.1.

Parameters:

metadata (dict) – Should be a list of application role connection metadata

Note

The bot token is required to update metadata.

generate_uri(metadata)

Creates an authorization uri with client information prefilled.

New in version 1.2.

Parameters:
  • scope (Union[str, list[str]]) – A list, or space-seperated string for the authorization scope

  • state (Optional[str]) – State parameter. It is recommended for security.

  • skip_prompt (Optional[bool]) – Doesn’t require the end user to reauthorize if they’ve already authorized you app before. Defaults to False.

  • response_type (Optional[Literal["code", "token"]]) – either code, or token. token means the server can’t access it, but the client can use it without converting.

  • guild_id (Optional[Union[int, str]]) – the guild ID to add a bot/webhook.

  • disable_guild_select (Optional[bool]) – wether to allow the authorizing user to change the selected guild

  • permissions (Optional[int]) – the permission bitwise integer for the bot being added.