Expand description
OAuth2 helpers: the client credentials of an application (D-132) and authorization URLs (D-133).
The token endpoints themselves are methods of Rest:
exchange_code,
refresh_access_token,
client_credentials_grant and
revoke_token.
§Examples
The authorization code grant, from the URL a user opens to the user’s own client:
use djs_model::{id::ApplicationId, oauth2::Scope};
use djs_rest::{
Rest,
oauth2::{AuthorizeUrl, OAuth2Credentials},
};
let client_id = ApplicationId::new(157_730_590_492_196_864);
let redirect_uri = "https://example.com/callback";
// 1. Send the user to the authorization page.
let url = AuthorizeUrl::code(client_id, redirect_uri)
.scopes([Scope::Identify, Scope::Guilds])
.state("a-random-value-tied-to-the-session")
.build();
// 2. Discord redirects back with `?code=…&state=…`; exchange the code.
let credentials = OAuth2Credentials::new(client_id, std::env::var("CLIENT_SECRET").unwrap());
let token = rest.exchange_code(&credentials, code, redirect_uri).await?;
// 3. Act for the user.
let user_rest = Rest::builder().bearer_token(token.access_token).build();
let user = user_rest.get_current_user().await?;Structs§
- Authorize
Url - Builds an authorization URL
(
https://discord.com/oauth2/authorize?…) that adds a bot to a guild or asks a user to authorize the application (D-133). - OAuth2
Credentials - An application’s client ID and client secret, which authenticate the token endpoints (D-132).
Enums§
- Prompt
- How the authorization page treats a user who already authorized the same scopes.
- Token
Type Hint - What kind of token
revoke_tokenrevokes. Discord revokes every token of the authorization either way.
Constants§
- AUTHORIZE_
URL - The base authorization URL.