处理身份验证令牌
ProcessAuthenticationTokens
用于处理 Unity Authentication 服务访问令牌,并使这些令牌可供游戏中集成的其他需要玩家进行身份验证的 UGS SDK 使用。由于访问令牌会到期,因此必须手动刷新访问令牌并使用新的访问令牌调用 ProcessAuthenticationTokens
。您可以使用访问令牌和会话令牌来调用 ProcessAuthenticationTokens
。Unity Authentication SDK 会在访问令牌到期之前刷新令牌,保持玩家会话处于活动状态,并允许登录缓存的玩家。
using Unity.Services.Authentication;
void SignUserWithCustomTokenWithAutoRefresh()
{
try
{
// Check if a cached player already exists by checking if the session token exists
if (AuthenticationService.Instance.SessionTokenExists)
{
// This call will sign in the cached player.
await AuthenticationService.Instance.SignInAnonymouslyAsync();
Debug.Log("Cached user sign in succeeded!");
}
else
{
var userTokens = // Fetch the user tokens using your method calls
AuthenticationService.Instance.ProcessAuthenticationTokens(userTokens.AccessToken, userTokens.SessionToken)
}
// Shows how to get the playerID
Debug.Log($"PlayerID: {AuthenticationService.Instance.PlayerId}");
}
catch (AuthenticationException ex)
{
// Compare error code to AuthenticationErrorCodes
// Notify the player with the proper error message
Debug.LogException(ex);
}
catch (RequestFailedException ex)
{
// Compare error code to CommonErrorCodes
// Notify the player with the proper error message
Debug.LogException(ex);
}
catch (Exception ex) {
// Handle exceptions from your method call
Debug.LogException(ex);
}
}
请注意代码样本仅描述了一种方法,而不代表一类方法。