Access
Connect cross-platform accounts & identity management
You can use AccelByte Cloud’s Java SDK to implement our backend services within your game. The SDK acts as a bridge between your game and our services. This guide will show you how to create a project which uses Java Server SDK from scratch.
AB_BASE_URL
environment variable.Confidential
.
Client ID
value for AB_CLIENT_ID
environment variable.Client Secret
value for AB_CLIENT_SECRET
environment variable.Create a folder and use gradle init
to create a Java project.
$ mkdir myproject
$ cd myproject/
$ gradle init --type java-application --dsl groovy --test-framework junit-jupiter
Welcome to Gradle 7.4.2!
Here are the highlights of this release:
- Aggregated test and JaCoCo reports
- Marking additional test source directories as tests in IntelliJ
- Support for Adoptium JDKs in Java toolchains
For more details see https://docs.gradle.org/7.4.2/release-notes.html
Starting a Gradle Daemon (subsequent builds will be faster)
Generate build using new APIs and behavior (some features may change in the next minor release)? (default: no) [yes, no]
Project name (default: myproject):
Source package (default: myproject):
> Task :init
Get more help with your project: https://docs.gradle.org/7.4.2/samples/sample_building_java_applications.html
BUILD SUCCESSFUL in 19s
2 actionable tasks: 2 executed
Add the required configuration in build.gradle
.
Replace {VERSION}
with a specific release version tag (opens new window) without the leading v
character.
// build.gradle
repositories {
mavenCentral()
...
}
dependencies {
...
implementation 'net.accelbyte.sdk:sdk:{VERSION}'
}
TIP
We recommended using the Java Server SDK version that matches your AccelByte Cloud version.
Create an SDK instance, log in using client credentials, and call an AccelByte Cloud API in App.java
.
The DefaultConfigRepository
gets its values from AB_BASE_URL
, AB_CLIENT_ID
, and AB_CLIENT_SECRET
environment variables.
// App.java
package myproject;
import net.accelbyte.sdk.api.iam.models.OauthmodelCountryLocationResponse;
import net.accelbyte.sdk.api.iam.operations.o_auth2_0_extension.GetCountryLocationV3;
import net.accelbyte.sdk.api.iam.wrappers.OAuth20Extension;
import net.accelbyte.sdk.core.AccelByteSDK;
import net.accelbyte.sdk.core.client.OkhttpClient;
import net.accelbyte.sdk.core.repository.DefaultConfigRepository;
import net.accelbyte.sdk.core.repository.DefaultTokenRepository;
public class App {
public static void main(String[] args) throws Exception {
// Create default HTTP client, token repository, and config repository instance
OkhttpClient httpClient = new OkhttpClient();
DefaultTokenRepository tokenRepository = new DefaultTokenRepository();
DefaultConfigRepository configRepository = new DefaultConfigRepository();
// Create SDK instance
AccelByteSDK sdk = new AccelByteSDK(
httpClient,
tokenRepository,
configRepository);
// Login using client credentials
boolean isLoginOk = sdk.loginClient();
if (!isLoginOk) {
System.exit(1); // Login failed
}
// Call an AccelByte Cloud endpoint e.g. GetCountryLocationV3
OAuth20Extension wrapper = new OAuth20Extension(sdk);
GetCountryLocationV3 operation = GetCountryLocationV3.builder()
.build();
OauthmodelCountryLocationResponse response = wrapper.getCountryLocationV3(operation);
System.out.println(response.getCountryName());
}
}
Set the required environment variables and run the code using gradle run
.
$ export AB_BASE_URL="https://demo.accelbyte.io" # AccelByte Cloud Base URL e.g. demo environment
$ export AB_CLIENT_ID="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # AccelByte Cloud OAuth Client ID
$ export AB_CLIENT_SECRET="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # AccelByte Cloud OAuth Client Secret
$ gradle run
You can now start using the following AccelByte services in your app.
See our Java SDK example repo (opens new window)for a selection of test cases you can use to customize your game. You can also view the Java Server SDK README.md (opens new window) for more information.
SDK reference (opens new window)
import net.accelbyte.sdk.api.iam.models.*;
import net.accelbyte.sdk.api.iam.operations.*;
import net.accelbyte.sdk.api.iam.wrappers.*;
SDK reference (opens new window)
import net.accelbyte.sdk.api.basic.models.*;
import net.accelbyte.sdk.api.basic.operations.*;
import net.accelbyte.sdk.api.basic.wrappers.*;
SDK reference (opens new window)
import net.accelbyte.sdk.api.social.models.*;
import net.accelbyte.sdk.api.social.operations.*;
import net.accelbyte.sdk.api.social.wrappers.*;
SDK reference (opens new window)
import net.accelbyte.sdk.api.platform.models.*;
import net.accelbyte.sdk.api.platform.operations.*;
import net.accelbyte.sdk.api.platform.wrappers.*;
SDK reference (opens new window)
import net.accelbyte.sdk.api.group.models.*;
import net.accelbyte.sdk.api.group.operations.*;
import net.accelbyte.sdk.api.group.wrappers.*;
SDK reference (opens new window)
import net.accelbyte.sdk.api.cloudsave.models.*;
import net.accelbyte.sdk.api.cloudsave.operations.*;
import net.accelbyte.sdk.api.cloudsave.wrappers.*;
SDK reference (opens new window)
import net.accelbyte.sdk.api.dsmc.models.*;
import net.accelbyte.sdk.api.dsmc.operations.*;
import net.accelbyte.sdk.api.dsmc.wrappers.*;
SDK reference (opens new window)
import net.accelbyte.sdk.api.sessionbrowser.models.*;
import net.accelbyte.sdk.api.sessionbrowser.operations.*;
import net.accelbyte.sdk.api.sessionbrowser.wrappers.*;
SDK reference (opens new window)
import net.accelbyte.sdk.api.lobby.models.*;
import net.accelbyte.sdk.api.lobby.operations.*;
import net.accelbyte.sdk.api.lobby.wrappers.*;
SDK reference (opens new window)
import net.accelbyte.sdk.api.achievement.models.*;
import net.accelbyte.sdk.api.achievement.operations.*;
import net.accelbyte.sdk.api.achievement.wrappers.*;
SDK reference (opens new window)
import net.accelbyte.sdk.api.dslogmanager.models.*;
import net.accelbyte.sdk.api.dslogmanager.operations.*;
import net.accelbyte.sdk.api.dslogmanager.wrappers.*;
SDK reference (opens new window)
import net.accelbyte.sdk.api.ugc.models.*;
import net.accelbyte.sdk.api.ugc.operations.*;
import net.accelbyte.sdk.api.ugc.wrappers.*;
SDK reference (opens new window)
import net.accelbyte.sdk.api.leaderboard.models.*;
import net.accelbyte.sdk.api.leaderboard.operations.*;
import net.accelbyte.sdk.api.leaderboard.wrappers.*;
SDK reference (opens new window)
import net.accelbyte.sdk.api.gdpr.models.*;
import net.accelbyte.sdk.api.gdpr.operations.*;
import net.accelbyte.sdk.api.gdpr.wrappers.*;
SDK reference (opens new window)
import net.accelbyte.sdk.api.legal.models.*;
import net.accelbyte.sdk.api.legal.operations.*;
import net.accelbyte.sdk.api.legal.wrappers.*;
SDK reference (opens new window)
import net.accelbyte.sdk.api.matchmaking.models.*;
import net.accelbyte.sdk.api.matchmaking.operations.*;
import net.accelbyte.sdk.api.matchmaking.wrappers.*;