Simple and lightweight Java wrapper for Discord's OAuth2 API.
- OAuth2 endpoints
- Fetch Access Token
/oauth2/token
- Refresh Access Token
/oauth2/token
- Revoke Access Token
/oauth2/token/revoke
- Fetch Access Token
- API endpoints
- Get User data
/users/@me
- Get User data
- Authorization URL creation
More API endpoints will be added in the future, if requested.
Simple webserver for handling the redirect URI will be added in the future, if requested.
// Creating DiscordApplication instance
DiscordApplication.Builder builder = new DiscordApplication.Builder();
builder.withDiscordApiUrl(DISCORD_API_URL) // (1)
.withClientId(CLIENT_ID) // Required, (2)
.withClientSecret(CLIENT_SECRET) // Required, (3)
.withRedirectUri(REDIRECT_URI) // Required, (4)
.withAuthorizationUrlFactory(DiscordOAuthAuthorizationUrlFactory) // (5)
.withScopes(SCOPES); // (6)
DiscordApplication discordApplication = builder.build();
// Creating DiscordOAuth instance
DiscordOAuth discordOAuth = new DiscordOAuth(discordApplication);
// Fetching access token
DiscordAccessToken tokens = discordOAuth.fetchAccessToken("code").send(); // (7)
// Refreshing access token
DiscordAccessToken tokens = discordOAuth.refreshAccessToken("refresh_token").send();
// Revoking access token
discordOAuth.revokeTokens("access_token").send();
Notes:
- This defaults to
DiscordApi.DEFAULT_API_URL
. However, you can specify your own URL if you want to. - This is the client ID of your application. You can find it in the Discord Developer Portal.
- This is the client secret of your application. You can find it in the Discord Developer Portal.
- This is the redirect URI of your application. You can find it in the Discord Developer Portal.
- This is the factory that is used for creating the authorization URL. You can use the default implementation or create your own by overriding the class.
- These are the scopes that you want to request from the user. You can find the list of all scopes here.
- This is the code that you get from the redirect URI, after the user authorizes your application and Discord redirects them there.
Asynchronous requests are also supported. You can find more information about them in the Fetching access/refresh token.
- Java >= 11
repositories {
mavenCentral()
}
dependencies {
// Library itself
implementation 'dev.mayuna:java-discord-oauth2-api:VERSION'
// Required for the library to work
implementation 'dev.mayuna:simple-java-api-wrapper:2.2'
implementation 'com.google.code.gson:gson:2.10'
}
<!-- Library itself -->
<dependency>
<groupId>dev.mayuna</groupId>
<artifactId>java-discord-oauth2-api</artifactId>
<version>VERSION</version>
</dependency>
<!-- Required for the library to work -->
<dependency>
<groupId>dev.mayuna</groupId>
<artifactId>simple-java-api-wrapper</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10</version>
</dependency>
You can find the latest version here.
DiscordApplication
is used in the DiscordOAuth
constructor. It holds the client ID, client secret, redirect URI, and
other various information about the application.
You must specify at least the client ID, client secret, and redirect URI.
DiscordApplication.Builder builder = new DiscordApplication.Builder();
builder.withDiscordApiUrl(DISCORD_API_URL)
.withClientId(CLIENT_ID)
.withClientSecret(CLIENT_SECRET)
.withRedirectUri(REDIRECT_URI)
.withAuthorizationUrlFactory(DiscordOAuthAuthorizationUrlFactory)
.withScopes(SCOPES);
DiscordApplication discordApplication = builder.build();
Using constructor (not recommended)
DiscordApplication discordApplication = new DiscordApplication(
DISCORD_API_URL,
CLIENT_ID,
CLIENT_SECRET,
REDIRECT_URI,
DiscordOAuthAuthorizationUrlFactory,
SCOPES
);
DiscordApplication
allows you to create the authorization URL that will user use to authorize your application.
DiscordApplication discordApplication = /* ... */;
String url;
// You will mostlikely use this one
url = discordApplication.createAuthorizationUrl("some-state");
url = discordApplication.createAuthorizationUrl("some-state", "some-prompt");
url = discordApplication.createAuthorizationUrl();
DiscordOAuthAuthorizationUrlFactory Info
As you can see in the previous section, you can specify the DiscordOAuthAuthorizationUrlFactory
in
the DiscordApplication
constructor/builder.
This class is used for creating the authorization URL. You can use the default implementation or create your own by overriding the class.
DiscordOAuth
is used for getting the access/refresh token from Discord's API.
DiscordApplication discordApplication = /* ... */;
DiscordOAuth discordOAuth = new DiscordOAuth(discordApplication);
code
is the code that you get from the redirect URI, after the user authorizes your application and Discord redirects
them there.
DiscordOAuth discordOAuth = /* ... */;
// Synchonous
DiscordAccessToken tokens = discordOAuth.fetchAccessToken("code").send();
// Asynchrounous
discordOAuth.fetchAccessToken("code")
.sendAsync()
.whenCompleteAsync((tokens, throwable) -> {
if (throwable != null) {
// Handle error
return;
}
// Use tokens
});
DiscordOAuth discordOAuth = /* ... */;
// Synchonous
DiscordAccessToken tokens = discordOAuth.refreshAccessToken("refresh_token").send();
// Asynchrounous
discordOAuth.refreshAccessToken("refresh_token")
.sendAsync()
.whenCompleteAsync((tokens, throwable) -> {
if (throwable != null) {
// Handle error
return;
}
// Use tokens
});
DiscordOAuth discordOAuth = /* ... */;
// Synchonous
DiscordAccessToken tokens = discordOAuth.revokeTokens("access_token").send();
// Asynchrounous
discordOAuth.revokeTokens("access_token")
.sendAsync()
.whenCompleteAsync((tokens, throwable) -> {
if (throwable != null) {
// Handle error
return;
}
// Request completed
});
If the request fails, the send()
method will throw an exception. You can catch it and handle it.
If you use sendAsync()
, the exception will be passed into the CompletableFuture
that is returned.
Sometimes, Discord will return an error in the response. You can check if the request was successful by using
the getError()
DiscordOAuth discordOAuth = /* ... */;
DiscordAccessToken tokens = discordOAuth.revokeTokens("access_token").send();
if (tokens.getError() != null) {
System.out.println("Error: " + tokens.getError());
// Error description can be null even when error is not null
System.out.println("Error description: " + tokens.getErrorDescription());
}