Skip to content

Using the API

João Gabriel edited this page Aug 31, 2023 · 1 revision

Creating the OAuth handler

import balbucio.discordoauth.DiscordOAuth;

DiscordOAuth oauthHandler = new DiscordOAuth(clientID: String, clientSecret: String, redirectUri: String, scope: String[]);
DiscordOAuth oauthHandler = new DiscordOAuth(clientID: String, clientSecret: String, redirectUri: String, scope: List<String>);
DiscordOAuth oauthHandler = new DiscordOAuth(clientID: String, clientSecret: String, redirectUri: String, scope: SupportedScopes[]);

Generating the OAuth URL

String authURL = oauthHandler.getAuthorizationURL(state: String);

state will be ingored by passing null.

Generating the OAuth URL with CustomRequest

CustomRequest request = new CustomRequest(requestUri: String, state: String, scope: String[]);
CustomRequest request = new CustomRequest(requestUri: String, state: String, scope: List<String>);
CustomRequest request = new CustomRequest(requestUri: String, state: String, scope: SupportedScopes[]);
String authURL = oauthHandler.getAuthorizationURL(request: CustomRequest);

state will be ingored by passing null.

Authorizing the code

import balbucio.discordoauth.model.TokensResponse;

TokensResponse tokens = oauthHandler.getTokens(code: String);
String accessToken = tokens.getAccessToken();
String refreshToken = tokens.getRefreshToken();

Refreshing the Access Token

TokensResponse tokens = oauthHandler.refreshTokens(refresh_token: String);

Creating the API handler

import balbucio.discordoauth.DiscordAPI;

DiscordAPI api = new DiscordAPI(access_token: String);

The following API fetch calls will throw IOException (HttpStatusException) when access is denied due to invalid scope or expired token.

Fetching User

Scope identity is required. Scope email is required for email and verified values.

import balbucio.discordoauth.model.User;

User user = api.fetchUser();

Fetching Guilds

Scope guilds is required.

import balbucio.discordoauth.model.Guild;

List<Guild> guilds = api.fetchGuilds();

Fetching Connections

Scope connections is required.

import balbucio.discordoauth.model.Connection;

List<Connection> connections = api.fetchConnections();

Joining a guild

Scope guilds.join is required.
Make sure you have a bot in your OAuth application and that this bot is in the guild you want to add members to, to learn more click here.

api.joinGuild(guild: Guild, user: User, botToken: String);
// You can create a guild using just the ID

Joining a Group Channel

Scope gdm.join is required.
This function has not yet been tested, so it is recommended not to add it to production. Feel free to test it out.

GroupChannel channel = new GroupChannel(id: String);
api.joinGuild(channel: GroupChannel, user: User);