Package discord4j.oauth2
Class DiscordOAuth2Client
java.lang.Object
discord4j.oauth2.DiscordOAuth2Client
A client registration capable of making requests to the Discord API on behalf of a single user using
their OAuth2 access token.
If you use a DiscordOAuth2Server, you can retrieve working instances by defining a
DiscordOAuth2SuccessHandler. In other cases use a factory method depending on how your OAuth2 application
is structured:
createFromCode(RestClient, AuthorizationCodeGrantRequest)if you want to defer the token fetching process until the first authorized requestcreateFromToken(RestClient, long, String, AccessTokenData)if access token data is already available, like after callingOAuth2Service.exchangeAuthorizationCode(AuthorizationCodeGrantRequest)createFromCredentials(RestClient, ClientCredentialsGrantRequest)if you are a bot developer and want to issue a Bearer token on your behalf for testing purposes
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic DiscordOAuth2ClientcreateFromCode(RestClient restClient, discord4j.discordjson.json.AuthorizationCodeGrantRequest request) Create an OAuth2 client by completing an authorization code grant flow.static DiscordOAuth2ClientcreateFromCredentials(RestClient restClient, discord4j.discordjson.json.ClientCredentialsGrantRequest request) Create an OAuth2 client by performing a client credentials flow.static DiscordOAuth2ClientcreateFromToken(RestClient restClient, long clientId, String clientSecret, discord4j.discordjson.json.AccessTokenData data) Create an OAuth2 client with the rawAccessTokenDatareturned fromOAuth2Service.exchangeAuthorizationCode(AuthorizationCodeGrantRequest).<T> Mono<T>exchange(DiscordWebRequest request, Class<T> responseType) Execute a givenDiscordWebRequeston behalf of a user and mapping the response to the given type, using the credentials stored under this client.Return the current access token for this client.Mono<discord4j.discordjson.json.GuildApplicationCommandPermissionsData>getApplicationCommandPermissions(long applicationId, long guildId, long commandId) Fetches permissions for a specific command for your application in a guild.Mono<discord4j.discordjson.json.AuthorizationInfoData>Returns info about the current authorization.Mono<discord4j.discordjson.json.UserData>Returns the user object of the requesting account.Mono<discord4j.discordjson.json.MemberData>getCurrentUserGuildMember(long guildId) Returns a member object from the current user in the given guild.Flux<discord4j.discordjson.json.UserGuildData>getCurrentUserGuilds(Map<String, Object> queryParams) Returns a list of partial guild objects the requesting account is a member of.Return the JacksonObjectMappertied to this instance for JSON handling purposes.Flux<discord4j.discordjson.json.ConnectionData>Return a list ofConnectionDataobjects.Mono<discord4j.discordjson.json.GuildApplicationCommandPermissionsData>modifyApplicationCommandPermissions(long applicationId, long guildId, long commandId, discord4j.discordjson.json.ApplicationCommandPermissionsRequest request) Edits command permissions for a specific command for your application in a guild and returns a guild application command permissions object.withAuthorizedClient(DiscordWebRequest request) Prepare a givenDiscordWebRequeston behalf of a user, using the credentials stored under this client.
-
Method Details
-
createFromCode
public static DiscordOAuth2Client createFromCode(RestClient restClient, discord4j.discordjson.json.AuthorizationCodeGrantRequest request) Create an OAuth2 client by completing an authorization code grant flow. This is useful if you have a custom HTTP server that receives thecodeparameter. Build aAuthorizationCodeGrantRequestmaking sure to include the code parameter and the access token with refresh capabilities will be stored once the first request is made using one of the API methods in this class orwithAuthorizedClient(DiscordWebRequest).For an example server implementation, check
DiscordOAuth2Server, which uses this method.- Parameters:
restClient- a Discord REST API client for performing requestsrequest- an object with all parameters required to complete an authorization code grant request- Returns:
- a client that can work with a valid user token to perform API requests
-
createFromCredentials
public static DiscordOAuth2Client createFromCredentials(RestClient restClient, discord4j.discordjson.json.ClientCredentialsGrantRequest request) Create an OAuth2 client by performing a client credentials flow. This is a quick and easy way as a bot developer to get an access token for testing purposes. Build aClientCredentialsGrantRequestusing your application parameters and intendedScopevalues, separated by spaces.- Parameters:
restClient- a Discord REST API client for performing requestsrequest- an object with all parameters required to complete a client credentials grant request- Returns:
- a client that can work with a valid token for your user to perform API requests
-
createFromToken
public static DiscordOAuth2Client createFromToken(RestClient restClient, long clientId, String clientSecret, discord4j.discordjson.json.AccessTokenData data) Create an OAuth2 client with the rawAccessTokenDatareturned fromOAuth2Service.exchangeAuthorizationCode(AuthorizationCodeGrantRequest). Useful if a token is requested directly by your HTTP server.- Parameters:
restClient- a Discord REST API client for performing requestsclientId- your application's client IDclientSecret- your application's client secretdata- the access token object- Returns:
- a client that can work with a valid token to perform API requests
-
getAccessToken
Return the current access token for this client. Depending on the OAuth2 scopes used, you can extract information from this object to perform additional tasks. For instance, if you usedScope.BOT,AccessToken.getGuild()will be available; if you usedScope.WEBHOOK_INCOMING,AccessToken.getWebhook()will be available.- Returns:
- the current access token authorized for this client. This is an immutable instance, so you may need to call this again if the token is refreshed.
- Throws:
IllegalStateException- if this access token was invalidated, indicating you might require reauthorization
-
getObjectMapper
Return the JacksonObjectMappertied to this instance for JSON handling purposes.- Returns:
- an object that can provide JSON processing
-
getAuthorizationInfo
Returns info about the current authorization. UseswithAuthorizedClient(DiscordWebRequest)to retrieve and use the Bearer token tied to this client.- Returns:
- a Mono with authorization details given by this token in this client, or an error Mono in case any request fails
-
getCurrentUser
Returns the user object of the requesting account. For OAuth2, this requires theScope.IDENTIFYscope, which will return the object without an email, and optionally theScope.EMAILscope, which returns the object with an email.- Returns:
- a Mono with user details if successful, otherwise an error Mono
-
getCurrentUserGuilds
public Flux<discord4j.discordjson.json.UserGuildData> getCurrentUserGuilds(Map<String, Object> queryParams) Returns a list of partial guild objects the requesting account is a member of. Requires theScope.GUILDSscope.- Parameters:
queryParams- optional query parameters for this endpoint, allowing pagination usingbefore,afterandlimit(defaults to 200)- Returns:
- a Flux with partial guild information for the user, otherwise an error Flux
-
getCurrentUserGuildMember
Returns a member object from the current user in the given guild. Request theScope.GUILDS_MEMBERS_READscope.- Parameters:
guildId- the guild to query the current user member object- Returns:
- a Mono with member information, otherwise an error Mono
-
getUserConnections
Return a list ofConnectionDataobjects. Requires this client was authorized to use theScope.CONNECTIONSscope. UseswithAuthorizedClient(DiscordWebRequest)to retrieve and use the Bearer token tied to this client.- Returns:
- a Mono with user connections, or an error Mono in case any request fails
-
getApplicationCommandPermissions
public Mono<discord4j.discordjson.json.GuildApplicationCommandPermissionsData> getApplicationCommandPermissions(long applicationId, long guildId, long commandId) Fetches permissions for a specific command for your application in a guild. Returns a guild application command permissions object.- Parameters:
applicationId- your application IDguildId- the guild IDcommandId- the command ID- Returns:
- a Mono with command permissions object for the requested guild, or an error Mono in case a request fails
-
modifyApplicationCommandPermissions
public Mono<discord4j.discordjson.json.GuildApplicationCommandPermissionsData> modifyApplicationCommandPermissions(long applicationId, long guildId, long commandId, discord4j.discordjson.json.ApplicationCommandPermissionsRequest request) Edits command permissions for a specific command for your application in a guild and returns a guild application command permissions object. You can add up to 100 permission overwrites for a command.- Parameters:
applicationId- your application IDguildId- the guild IDcommandId- the command IDrequest- a request body containing the permissions to be set- Returns:
- a Mono with command permissions object for the requested guild, or an error Mono in case a request fails
-
exchange
Execute a givenDiscordWebRequeston behalf of a user and mapping the response to the given type, using the credentials stored under this client. The token fetching, refreshing (if required) and API request are run once this Mono is subscribed. For more control on the authorized request and response seewithAuthorizedClient(DiscordWebRequest).- Type Parameters:
T- the response type- Parameters:
request- the compiled Discord REST API request to be run on behalf of a userresponseType- the expected response type from the API- Returns:
- a Mono with the mapped response if successful, otherwise an error Mono
-
withAuthorizedClient
Prepare a givenDiscordWebRequeston behalf of a user, using the credentials stored under this client. The token fetching, refreshing (if required) and API request are run once this Mono is subscribed.- Parameters:
request- the compiled Discord REST API request to be run on behalf of a user- Returns:
- a Mono of a request including required steps to include proper authorization
-