基于接口实现的令牌生成
How to generate access tokens using an interface implementation.
阅读时间2 分钟最后更新于 7 个月前
如果要控制 Vivox 访问令牌 (VAT) 的生成方式,请创建一个实现 Vivox SDK 的 接口的类,并将这个类注册到 VivoxService。
IVivoxTokenProvider如果通过调用 来注册 IVivoxTokenProvider 的实现,则每当需要令牌进行登录等操作时,Vivox 都会调用您的实现中的 方法。
VivoxService.Instance.SetTokenProviderIVivoxTokenProvider.GetTokenAsync创建有效负载所需的信息作为覆盖的 方法的输入提供。此信息在制作 VAT 时使用。
IVivoxTokenProvider.GetTokenAsync您仍然需要设置服务器端令牌供应,并在覆盖的 方法中获取令牌。
IVivoxTokenProvider.GetTokenAsync订阅
在登录您的用户之前,您必须使用以下方法注册您的 实现:
IVivoxTokenProviderVivoxService.Instance.SetTokenProvider(new CustomTokenProvider());
获取令牌
应使用覆盖的方法中提供的所有参数(即使有些参数为空)创建有效负载,并将有效负载发送到您的安全服务器以生成您的 Vivox 访问令牌。最佳做法是发送所有参数;仅返回有效负载所需的内容。使用有效负载作为 方法的输入。
GetTokenAsync对于某些令牌,不同的参数将为空。例如,登录令牌的 targetUserUri 和 channelUri 将为空。这是预期的行为。
以下是一个令牌生成示例。
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 }}
如需了解有关此特定流程的更多详细信息,请参阅使用 Vivox 访问令牌登录。
要了解有关生成服务器端令牌的更多信息,请参阅在安全服务器上生成令牌。