Developer Console

Leaderboards and Tournaments Tutorial - Player Registration & Authentication

Return to Tutorial Overview


It is time to get into the actual integration with Leaderboards and Tournaments. We could implement WS calls with an HTTP library to call the Leaderboards and Tournaments APIs, but that would take a lot of effort. Instead, we use the openapi-codegen tool to generate the client code for us. After downloading the Leaderboards and Tournaments Player API model, we will generate the client code with this command:

java -jar openapi-generator-cli.jar generate -i player-api.json -g kotlin -o GameOnAPI/ --artifact-id PlayerAPI --type-mappings number,kotlin.Double  --language-specific-primitives kotlin.Unit --additional-properties dateLibrary=java8

The generator should create a new source project in the GameOnAPI directory. Import this into our project via the app build.gradle file like this:

dependencies {
    ...

    implementation project(":GameOnAPI")
}

In addition, we need to add a reference to the generated code in the root gradle.settings file:

include ':app'
include ':GameOnAPI'

Now we can reference a class called org.openapitools.client.apis.DefaultApi that contains functions for all of the Leaderboards and Tournaments Game APIs.

The first task is to implement an AsyncTask to register and authenticate the player. The registration call requires us to generate our own ID for the player and call Register Player. We get the API key from the Leaderboards and Tournaments console:

val playerId = UUID.randomUUID().toString()

val registerResponse = gameOnApi.registerPlayerUsingPOST(
        RegisterPlayerRequest(vendorPlayerId = playerId),
        apiKey
)

The response to this request will give us a playerToken. We use this token to authenticate the player. Registration should be called only once per player, so it's best to save the playerToken somewhere and call Register Player only if the token doesn't yet exist.

Now that we have a playerToken, we can use it to authenticate:

val authResponse = gameOnApi.authPlayerUsingPOST(
                    AuthPlayerRequest(
                            playerToken = loadPlayerToken(),
                            playerName = enteredPlayerName,
                            appBuildType = "development",
                            deviceOSType = "android"
                    ), apiKey)

The loadPlayerToken() function simply loads the value from shared prefs. The full code to this AsyncTask is available in the step-3 branch, as com.amazon.gameon.fruitquiz.api.AuthenticateGameOnTask:

We will hold off on calling this task from our splash activity until we've implemented the enter tournament logic, which is the next step.

Next - Enter Tournament or Match