Interface implementation-based token generation

To control how Vivox Access Tokens (VATs) are generated, create a class that implements the Vivox SDK’s IVivoxTokenProvider interface and register it with the VivoxService.

When you register your implementation of IVivoxTokenProvider by calling VivoxService.Instance.SetTokenProvider, Vivox will call your implementation's IVivoxTokenProvider.GetTokenAsync method whenever a token is needed for actions such as logging in.

Note: A best practice reccomendation for token generation is to use Unity Cloud Code to generate your tokens. For further details on using Cloud Code for token generation, review the token API documentation.

The information needed to create a payload is provided as input to the overridden IVivoxTokenProvider.GetTokenAsync method. This information is used when crafting a VAT.

You will still need to set up server-side token vending and fetch the token within the overridden IVivoxTokenProvider.GetTokenAsync method.

To subscribe

Before logging in your user, you must register your IVivoxTokenProvider implementation using:

VivoxService.Instance.SetTokenProvider(new CustomTokenProvider());

Important: The subscribe flow changed in v16.5.0. For Unity Vivox SDK versions previous to 16.5.0, you must register your IVivoxTokenProvider implementation before initializing the Vivox SDK.

Fetch the token

Create the payload with all of the parameters provided in the overridden method, even if some are empty, and send it to your secure server to generate your Vivox Access Token. Best practice is to send all the parameters; only what’s needed for the payload will be returned. Use the payload as input into the GetTokenAsync method.

For certain tokens, different parameters will be empty. For example, targetUserUri and channelUri will be empty for a login token. This is expected behaviour.

The following is an example of token generation:

public class VoiceManager : MonoBehaviour
{
    async void Start()
    {
        // Must be done before any other Vivox action otherwise tokens will not be generated properly.
        VivoxService.Instance.SetTokenProvider(new VivoxTokenProvider());
        await UnityServices.InitializeAsync();
        await VivoxService.Instance.InitializeAsync();
    }
}

class VivoxTokenProvider : IVivoxTokenProvider
{
    public Task<string> GetTokenAsync(string issuer = null, TimeSpan? expiration = null, string targetUserUri = null, string action = null, string channelUri = null, string fromUserUri = null, string realm = null)
    {
        // Implement token fetching logic here.
        // The method parameters contain the necessary information for crafting the request payload.
        // This will be called whenever a token is needed for a Vivox action
    }
}

For more details on this specific flow, refer to Sign in with Vivox Access Token

To learn more about generating server-side tokens, refer to Generate a token on a secure server.