Session Token API

Authentication is a process in which the credentials provided are compared to those on file in a database of authorized users’ information on a local operating system or within an authentication server.

VIVEPORT provides the security way to allow the application developer to verify the end user’s ID between VIVEPORT server and application server with authentication feature.

How to use Session Token API

Note

Remember that you should call Top Level API - Api.Init() and Session Token API - Token.IsReady() to setup the SDK library and client runtime before you call the GetSessionToken() API. And please remember to call Top Level API - Api.Shutdown() to teardown the library runtime after you finish using VIVEPORT SDK.

GetSessionToken() : You can call this API to get the user’s token. The VIVEPORT server will release the token while VIVEPORT end user login the content. The service will bring this token with the content service token to VIVEPORT to compare. If those 2 tokens feedback is positive, the end user will be allowed to play the content.

Attention

Please check the document link to get the server authentication and other detailed information and process.

Important

Please see the Error Codes and Trouble Shooting pages to know what to do when you got problems using the VIVEPORT SDK.

Detailed Example in Unity

using UnityEngine;
using System;
using Viveport;

/** Language: C# */
public class ViveportDemo : MonoBehaviour
{
   static string VIVEPORT_ID = "bd67b286-aafc-449d-8896-bb7e9b351876";

   // Use this for initialization
   void Start ()
   {
       Api.Init(InitStatusHandler, VIVEPORT_ID);
   }

   private static void InitStatusHandler(int nResult)
   {
       Viveport.Core.Logger.Log("Init(): " + nResult);
       if (nResult != 0)
       {
          Viveport.Core.Logger.Log("Init setup error, please close the content...");
          // Handle error
          return;
       }
       Token.IsReady(IsTokenReadyHandler);
   }

   private static void IsTokenReadyHandler(int nResult)
   {
       if (nResult != 0)
       {
          Viveport.Core.Logger.Log("Platform setup error, please close the content ...");
          // Handle error
          return;
       }
       Token.GetSessionToken(GetSessionTokenHandler);
   }

   private static void GetSessionTokenHandler(int nResult, string message)
   {
       if (nResult != 0)
       {
          Viveport.Core.Logger.Log("GetSessionTokenHandler error: " + nResult + ", message:" + message);
          // Handle error
          return;
       }
       Viveport.Core.Logger.Log("GetSessionTokenHandler is successful, token:" + message);
       // Do more things
   }
}

Detailed Example in Unreal

ViveportTokenDemo.h

#include "Components/ActorComponent.h"
#include "ViveportApi.h"
#include "ViveportToken.h"

#include "ViveportTokenDemo.generated.h"

UCLASS(ClassGroup=(Viveport), meta=(BlueprintSpawnableComponent))
class VIVEPORTSDK_API UViveportTokenDemo : public UActorComponent
{
    GENERATED_BODY()

public:
    // Sets default values for this component's properties
    UViveportTokenDemo();

    // Called when the game starts
    void BeginPlay() override;

    void EndPlay(const EEndPlayReason::Type EndPlayReason) override;

    FString APP_ID = "bd67b286-aafc-449d-8896-bb7e9b351876";

    class MyIsTokenReadyCallback : public ViveportApiStatus
    {
    public:
        void OnSuccess(
            ) override;
        void OnFailure(int error_code
            ) override;
    };

    class MyGetSessionTokenCallback : public ViveportApiStatus2
    {
    public:
        void OnSuccess(
            const FString& token
            ) override;
        void OnFailure(
            int errorCode,
            const FString& errorMessage
            ) override;
    };

private:
    // callback interface
    class MyInitCallback : public ViveportApiStatus
    {
    public:
        void OnSuccess(
            ) override;
        void OnFailure(int error_code
            ) override;
    };

    MyInitCallback myInitCallback;

    class MyShutdownCallback : public ViveportApiStatus
    {
    public:
        void OnSuccess(
            ) override;
        void OnFailure(int error_code
            ) override;
    };

    MyShutdownCallback myShutdownCallback;
};

ViveportTokenDemo.cpp

#include "ViveportSDKPrivatePCH.h"
#include "ViveportTokenDemo.h"

UViveportTokenDemo::MyIsTokenReadyCallback myIsTokenReadyCallback;
UViveportTokenDemo::MyGetSessionTokenCallback myGetSessionTokenCallback;

// Sets default values for this component's properties
UViveportTokenDemo::UViveportTokenDemo()
{
    // Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
    // off to improve performance if you don't need them.
    PrimaryComponentTick.bCanEverTick = true;
}

// Called when the game starts
void UViveportTokenDemo::BeginPlay()
{
    Super::BeginPlay();

    UViveportApi::Init(&myInitCallback, APP_ID);
}

void UViveportTokenDemo::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
    Super::EndPlay(EndPlayReason);

    UViveportApi::Shutdown(&myShutdownCallback);
}

void UViveportTokenDemo::MyInitCallback::OnSuccess()
{
    FString fstring("Init success.");
    GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::White, fstring);

    UViveportToken::IsReady(&myIsTokenReadyCallback);
}

void UViveportTokenDemo::MyInitCallback::OnFailure(int error_code)
{
    FString fstring = FString::Printf(TEXT("Init failure. error=%d"), error_code);
    GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::White, fstring);
}

void UViveportTokenDemo::MyIsTokenReadyCallback::OnSuccess()
{
    FString fstring("IsTokenReady success.");
    GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::White, fstring);

    UViveportToken::GetSessionToken(&myGetSessionTokenCallback);
}

void UViveportTokenDemo::MyIsTokenReadyCallback::OnFailure(int error_code)
{
    FString fstring = FString::Printf(TEXT("IsTokenReady failure. error=%d"), error_code);
    GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::White, fstring);
}

void UViveportTokenDemo::MyGetSessionTokenCallback::OnSuccess(
    const FString& token
    )
{
    FString fstring = FString::Printf(TEXT("Session Token=%s"), *token);
    GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::White, fstring);
}

void UViveportTokenDemo::MyGetSessionTokenCallback::OnFailure(int error_code, const FString& error_message) {
    FString fstring = FString::Printf(TEXT("Get session token failed! error_code=%d"),
        error_code);
    if (!error_message.IsEmpty())
    {
        fstring += FString::Printf(TEXT(",\n error_message=%s"),
            *error_message);
    }

    GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::White, fstring);
}

void UViveportTokenDemo::MyShutdownCallback::OnSuccess()
{
    FString fstring("Shutdown success.");
    GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::White, fstring);
}

void UViveportTokenDemo::MyShutdownCallback::OnFailure(int error_code)
{
    FString fstring = FString::Printf(TEXT("Shutdown failure. error=%d"), error_code);
    GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::White, fstring);
}