Getting Started with the Enjin Platform SDKs

Authenticating a Project Client

Creating the Client

The first step we take in setting up our ProjectClient is to instantiate it. To do so we must utilize a builder class nested within the client.
Java
C# | Unity
C++
import com.enjin.sdk.ProjectClient;
import com.enjin.sdk.ProjectClient.ProjectClientBuilder;
ProjectClientBuilder builder = ProjectClient.builder();
using static Enjin.SDK.ProjectClient;
ProjectClientBuilder builder = Builder();
#include "enjinsdk/ProjectClient.hpp"
using namespace enjin::sdk;
ProjectClient::ProjectClientBuilder builder = ProjectClient::builder();
Once we have the builder we may call its methods to configure it for the client we would like to create. One configuration we must set is the network our project exists on. Our choices are Mainnet, JumpNet, and Goerli. URLs for these networks can be programmatically acquired from the EnjinHosts class and passed to the builder.
Java
C# | Unity
C++
import com.enjin.sdk.EnjinHosts;
String mainnet = EnjinHosts.MAIN_NET;
String jumpnet = EnjinHosts.JUMP_NET;
String goerli = EnjinHosts.GOERLI;
builder.baseUri(/* Enjin host here */);
using Enjin.SDK;
System.Uri mainnet = EnjinHosts.MAIN_NET;
System.Uri jumpnet = EnjinHosts.JUMP_NET;
System.Uri goerli = EnjinHosts.GOERLI;
builder.BaseUri(/* Enjin host here */);
#include "enjinsdk/EnjinHosts.hpp"
#include "enjinsdk/ProjectClient.hpp"
#include <memory>
#include <string>
using namespace enjin::sdk;
std::string mainnet = EnjinHosts::MainNet;
std::string jumpnet = EnjinHosts::JumpNet;
std::string goerli = EnjinHosts::Goerli;
builder.base_uri(/* Enjin host here */);
After we finish configuring the builder, we then create the client by calling the builder's build method.
Java
C# | Unity
C++
import com.enjin.sdk.ProjectClient;
ProjectClient client = builder.build();
using Enjin.SDK;
ProjectClient client = builder.Build();
#include "enjinsdk/ProjectClient.hpp"
#include <memory>
using namespace enjin::sdk;
std::unique_ptr<ProjectClient> client = builder.build();
The configuration methods return the builder itself to allow for chaining calls to other methods.

Sending the Authentication Request

To authenticate our client, we will need our project's UUID and secret key. These may be found by navigating to the project page on the Enjin platform and found under Settings -> API Credentials.
You should make sure that you're only authenticating as a project in a secure environment. The project's secret enables a large degree of access over the project and is intended solely to be ran in an environment managed by the developer. It must not be exposed to others.
Once we have retrieved the UUID and secret, we may go about authenticating our client. For a project client we may either create the request ourselves to send to the platform or we may use the built-in convenience method to authenticate as shown directly below in the example.
Java
C# | Unity
C++
String uuid = "<the-project's-uuid>";
String secret = "<the-project's-secret>";
client.authClient(uuid, secret).get();
string uuid = "<the-project's-uuid>";
string secret = "<the-project's-secret>";
client.AuthClient(uuid, secret).Wait();
#include <string>
std::string uuid = "<the-project's-uuid>";
std::string secret = "<the-project's-secret>";
client->auth_client(uuid, secret).wait();

Creating the Authentication Request

Before creating the AuthProject request, we must first gather our project's UUID and secret key. To do so, we may navigate to our project page on the Enjin platform and find these items under Settings -> API Credentials.
Once the UUID and secret have been acquired, we can pass them to the AuthProject request we have created.
Java
C# | Unity
C++
import com.enjin.sdk.schemas.project.queries.AuthProject;
AuthProject req = new AuthProject()
.uuid("<the-project's-uuid>")
.secret("<the-project's-secret>");
using Enjin.SDK.ProjectSchema;
AuthProject req = new AuthProject()
.Uuid("<the-project's-uuid>")
.Secret("<the-project's-secret>");
#include "enjinsdk/project/AuthProject.hpp"
using namespace enjin::sdk::project;
AuthProject req = AuthProject()
.set_uuid("<the-project's-uuid>")
.set_secret("<the-project's-secret>");

Sending the Authentication Request

To send any request to the platform, we must call the method in our client whose name matches the request. The result of these methods in a synchronous operation will be a GraphQL response that wraps the data we want. An example of what this looks like for the AuthProject request is shown below:
Java
C# | Unity
C++
import com.enjin.sdk.graphql.GraphQLResponse;
GraphQLResponse<AccessToken> res = client.authProject(req).get();
using Enjin.SDK.Graphql;
GraphqlResponse<AccessToken> res = client.AuthProject(req).Result;
#include "enjinsdk/GraphqlResponse.hpp"
using namespace enjin::sdk::graphql;
GraphqlResponse<AccessToken> res = client->auth_project(req).get();
With the GraphQL response from the platform we can get the data contained within it. For authentication requests this will be a AccessToken class representing the platform's Auth type. Before retrieving the data however, we may want to consider checking if the response was successful. To do so, we can use the method shown below and get its result:
Java
C# | Unity
C++
boolean result = res.isSuccess();
bool result = res.IsSuccess;
bool result = res.is_successful();
Now that we have determined that the response was successful we can retrieve the AccessToken model from the response. This may be done with the code shown below:
Java
C# | Unity
C++
import com.enjin.sdk.models.AccessToken;
AccessToken accessToken = res.getData();
using Enjin.SDK.Models;
AccessToken accessToken = res.Result;
#include "enjinsdk/models/AccessToken.hpp"
using namespace enjin::sdk::models;
AccessToken access_token = res.get_result().value();

Authenticating

With the AccessToken we can now pass the string token contained within to the client's authentication method.
Java
C# | Unity
C++
client.auth(accessToken.getToken());
client.Auth(accessToken.Token);
client->auth(access_token.get_token().value());
After authenticating, we may check to see if the authentication was successful. We can do so with the method shown below and getting the Boolean value returned.
Java
C# | Unity
C++
boolean result = client.isAuthenticated();
bool result = client.IsAuthenticated;
bool result = client->is_authenticated();
With our client successfully authenticated we can now use it to make further requests to the platform. One point of note though is that our client's authentication with the platform will eventually expire. The time in seconds until the authentication expires can retrieved from the AccessToken as shown below:
Java
C# | Unity
C++
long time = accessToken.getExpiresIn();
long time = accessToken.ExpiresIn.Value;
long time = access_token.get_expires_in().value();

Automatic Reauthentication

The ProjectClient class may be setup to automatically reauthenticate itself before its access token expires. To do so we must first return to our client builder and make a call to its method which enables automatic reauthentication.
Java
C# | Unity
C++
builder.enableAutomaticReauthentication();
builder.EnableAutomaticReauthentication();
builder.enable_automatic_reauthentication();
After doing so we will then make a call to the client's authentication method using our project's UUID and secret key. This will allow our client to make the initial authentication request as well as cache the UUID and secret key that it will need to perform reauthentication on its own.
Java
C# | Unity
C++
String uuid = "<the-project's-uuid>";
String secret = "<the-project's-secret>";
client.authClient(uuid, secret).get();
string uuid = "<the-project's-uuid>";
string secret = "<the-project's-secret>";
client.AuthClient(uuid, secret).Wait();
#include <string>
std::string uuid = "<the-project's-uuid>";
std::string secret = "<the-project's-secret>";
client->auth_client(uuid, secret).wait();
In cases where automatic reauthentication is interrupted we may interface with our client to receive an event warning us that the process handling reauthentication has stopped. Examples for how to do so for each SDK are shown below:
Java
C# | Unity
C++
import com.enjin.sdk.IAuthenticationEventListener;
// As a configuration on the client builder
builder.authenticationListener(new IAuthenticationEventListener() {
@Override
public void onAutomaticReauthenticationStopped() {
// Handle the event
}
});
// As a event on the client
client.OnAutomaticReauthenticationStopped += (sender, args) =>
{
// Handle the event
};
// As a configuration on the client builder
builder.reauthentication_stopped_handler([]() {
// Handle the event
});

Authenticating a Player Client

To authenticate a player client we must first instantiate and authenticate a project client, which has access to the schemas we need. Read over the Authenticate a Project Client section for steps on setting up a project client ready to send requests to the platform.

Getting the Access Token

Currently, there are two ways to get the access token for authenticating a player client. One such way is through the CreatePlayer request, which is for new players that do not yet exist for our project. The other way to acquire a player's access token is via the AuthPlayer request, which is used for existing players.

Creating a New Player

When using the CreatePlayer request, we provide the ID of the new player we wish to add to our project as shown below:
Java
C# | Unity
C++
import com.enjin.sdk.schemas.project.mutations.CreatePlayer;
CreatePlayer req = new CreatePlayer()
.id("<the-player's-id>");
using Enjin.SDK.ProjectSchema;
CreatePlayer req = new CreatePlayer()
.Id("<the-player's-id>");
// Using a authenticated ProjectClient
GraphqlResponse<AccessToken> res = client.CreatePlayer(req).Result;
#include "enjinsdk/project/CreatePlayer.hpp"
using namespace enjin::sdk::project;
CreatePlayer req = CreatePlayer()
.set_id("<the-player's-id>");
With the request in hand, we can now send the CreatePlayer request to the platform and if successful we will get the player's access token in the response as shown below:
Java
C# | Unity
C++
import com.enjin.sdk.graphql.GraphQLResponse;
import com.enjin.sdk.models.AccessToken;
// Using a authenticated ProjectClient
GraphQLResponse<AccessToken> res = client.createPlayer(req).get();
AccessToken accessToken = res.getData();
using Enjin.SDK.Graphql;
using Enjin.SDK.Models;
// Using a authenticated ProjectClient
GraphqlResponse<AccessToken> res = client.CreatePlayer(req).Result;
AccessToken accessToken = res.Result;
#include "enjinsdk/GraphqlResponse.hpp"
#include "enjinsdk/models/AccessToken.hpp"
using namespace enjin::sdk::graphql;
using namespace enjin::sdk::models;
// Using a authenticated ProjectClient
GraphqlResponse<AccessToken> res = client->create_player(req).get();
AccessToken access_token = res.get_result().value();

Authenticating an Existing Player

When using the AuthPlayer request, we must specify the ID of the player whom we wish to authenticate for as shown below:
Java
C# | Unity
C++
import com.enjin.sdk.schemas.project.queries.AuthPlayer;
AuthPlayer req = new AuthPlayer()
.id("<the-player's-id>");
using Enjin.SDK.ProjectSchema;
AuthPlayer req = new AuthPlayer()
.Id("<the-player's-id>");
#include "enjinsdk/project/AuthPlayer.hpp"
using namespace enjin::sdk::project;
AuthPlayer req = AuthPlayer()
.set_id("<the-player's-id>");
With the request in hand, we can now send the AuthPlayer request to the platform and get the player's access token.
Java
C# | Unity
C++
import com.enjin.sdk.graphql.GraphQLResponse;
import com.enjin.sdk.models.AccessToken;
// Using a authenticated ProjectClient
GraphQLResponse<AccessToken> res = client.authPlayer(req).get();
AccessToken accessToken = res.getData();
using Enjin.SDK.Graphql;
using Enjin.SDK.Models;
// Using a authenticated ProjectClient
GraphqlResponse<AccessToken> res = client.AuthPlayer(req).Result;
AccessToken accessToken = res.Result;
#include "enjinsdk/GraphqlResponse.hpp"
#include "enjinsdk/models/AccessToken.hpp"
using namespace enjin::sdk::graphql;
using namespace enjin::sdk::models;
// Using a authenticated ProjectClient
GraphqlResponse<AccessToken> res = client->auth_player(req).get();
AccessToken access_token = res.get_result().value();

Authenticating the Client

Step 1: Create the Client

Creating a player client is similar to creating a project client, but instead of using the ProjectClient and its related classes we will be using the PlayerClient class.
Java
C# | Unity
C++
import com.enjin.sdk.EnjinHosts;
import com.enjin.sdk.PlayerClient;
String mainnet = EnjinHosts.MAIN_NET;
String jumpnet = EnjinHosts.JUMP_NET;
String goerli = EnjinHosts.GOERLI;
PlayerClient playerClient = PlayerClient
.builder()
.baseUri("<enjin-host-url>")
.build();
using Enjin.SDK;
System.Uri mainnet = EnjinHosts.MAIN_NET;
System.Uri jumpnet = EnjinHosts.JUMP_NET;
System.Uri goerli = EnjinHosts.GOERLI;
PlayerClient playerClient = PlayerClient
.Builder()
.BaseUri("<enjin-host-url>")
.Build();
#include "enjinsdk/PlayerClient.hpp"
#include <memory>
#include <string>
using namespace enjin::sdk;
std::string mainnet = EnjinHosts::MainNet;
std::string jumpnet = EnjinHosts::JumpNet;
std::string goerli = EnjinHosts::Goerli;
std::unique_ptr<PlayerClient> player_client = PlayerClient::builder()
.base_uri("<enjin-host-url>")
.build();

Step 2: Pass the String Token

Once the player client has been created, it may be authenticated with the access token we received from the platform using either the CreatePlayer or AuthPlayer requests. To do so, we call the respective authentication method and provide the string token contained within.
Java
C# | Unity
C++
playerClient.auth(accessToken.getToken());
playerClient.Auth(accessToken.Token);
player_client->auth(access_token.get_token().value());
As with the project client, we may check if the token was valid by using the method seen below and getting its Boolean value.
Java
C# | Unity
C++
boolean result = playerClient.isAuthenticated();
bool result = playerClient.IsAuthenticated;
bool result = player_client->is_authenticated();