Interface implementation-based token generation
How to generate access tokens using an interface implementation.
Read time 2 minutesLast updated 7 months ago
To control how Vivox Access Tokens (VATs) are generated, create a class that implements the Vivox SDK’s interface and register it with the VivoxService.
IVivoxTokenProviderWhen you register your implementation of IVivoxTokenProvider by calling , Vivox will call your implementation's method whenever a token is needed for actions such as logging in.
VivoxService.Instance.SetTokenProviderIVivoxTokenProvider.GetTokenAsyncThe information needed to create a payload is provided as input to the overridden method. This information is used when crafting a VAT.
IVivoxTokenProvider.GetTokenAsyncYou will still need to set up server-side token vending and fetch the token within the overridden method.
IVivoxTokenProvider.GetTokenAsyncTo subscribe
Before logging in your user, you must register your implementation using:
IVivoxTokenProviderVivoxService.Instance.SetTokenProvider(new CustomTokenProvider());
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 method.
GetTokenAsyncFor 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.