I’m trying to login using Google Play Games, which is successful but when I request a Token so I can create or link it to an anonymous account, the token is null even though the sign-in was successful.
I see the play games sign-in banner at the top of the screen.
I’m not sure what else to look at or check, any guidance would be greatly appreciated.
Logs:
Authentication - setting up events
Authentication - setting up token
Authentication - signing in
Authentication - attempting to login using google play games
Authentication - login successful, requesting token
Authentication - requesting token
Authentication - failed to request token, attempting to sign in anonymously
Authentication - attempting to sign in anonymously
Authentication - signed in
Authentication - sign in anonymously successful
using System;
using System.Threading.Tasks;
using GooglePlayGames;
using GooglePlayGames.BasicApi;
using Unity.Services.Authentication;
using Unity.Services.Core;
using UnityEngine;
public class AuthenticationManager : MonoBehaviour
{
public delegate void LoginCompleteHandler();
public event LoginCompleteHandler OnLoginComplete;
public delegate void LoginFailedHandler();
public event LoginFailedHandler OnLoginFailed;
private string _token;
private static AuthenticationManager _instance;
public static AuthenticationManager Instance
{
get { return _instance; }
}
private async void Awake()
{
if (_instance == null)
{
_instance = this;
DontDestroyOnLoad(gameObject);
}
else if (_instance != this)
{
Destroy(gameObject);
}
await UnityServices.InitializeAsync();
}
private async void Start()
{
SetupAuthenticationEvents();
SetupToken();
await SignInAsync();
}
private void SetupAuthenticationEvents()
{
Logger.Log("Authentication - setting up events");
AuthenticationService.Instance.SignedIn += () =>
{
Logger.Log($"Authentication - signed in");
OnLoginComplete?.Invoke();
};
AuthenticationService.Instance.SignInFailed += (err) =>
{
Logger.LogError($"Authentication - failed to sign in: {err}");
OnLoginFailed?.Invoke();
};
AuthenticationService.Instance.SignedOut += () =>
{
Logger.Log("Authentication - signed out");
};
AuthenticationService.Instance.Expired += async () =>
{
Logger.Log("Authentication - sign in expired");
OnLoginFailed?.Invoke();
};
}
private void SetupToken()
{
Logger.Log("Authentication - setting up token");
if (PlayerPrefs.HasKey("Token"))
{
_token = PlayerPrefs.GetString("Token");
}
}
private void SetToken(string token)
{
Logger.Log("Authentication - setting token");
PlayerPrefs.SetString("Token", token);
_token = token;
}
private async Task SignInAsync()
{
Logger.Log("Authentication - signing in");
#if UNITY_EDITOR
await SignInAnonymouslyAsync();
#endif
#if UNITY_ANDROID
await LoginWithGooglePlayGamesAsync();
#endif
#if UNITY_IOS
// TODO
#endif
}
private async Task LoginWithGooglePlayGamesAsync()
{
Logger.Log("Authentication - attempting to login using google play games");
if (!string.IsNullOrWhiteSpace(_token))
{
await SignInWithGooglePlayGamesAsync(_token);
}
else
{
PlayGamesPlatform.Instance.Authenticate(LoginWithGooglePlayGamesCallback);
}
}
private async void LoginWithGooglePlayGamesCallback(SignInStatus success)
{
if (success == SignInStatus.Success)
{
Logger.Log("Authentication - login successful, requesting token");
PlayGamesPlatform.Instance.RequestServerSideAccess(true, PlayGamesPlatformRequestServerSideAccessCallback);
}
else
{
Logger.LogError("Authentication - failed to login with google play games, attempting to sign in anonymously");
await SignInAnonymouslyAsync();
}
}
private async void PlayGamesPlatformRequestServerSideAccessCallback(string code)
{
Logger.Log("Authentication - requesting token");
if (string.IsNullOrWhiteSpace(code))
{
Logger.Log("Authentication - failed to request token, attempting to sign in anonymously");
await SignInAnonymouslyAsync();
}
else
{
Logger.Log("Authentication - requesting token successful");
SetToken(code);
LinkWithGooglePlayGamesAsync(code);
}
}
async Task SignInWithGooglePlayGamesAsync(string authCode)
{
try
{
await AuthenticationService.Instance.SignInWithGooglePlayGamesAsync(authCode);
Logger.Log("Authentication - play games login successful");
}
catch (Exception ex)
{
Logger.LogError($"Authentication - failed to sign in with google play games using token: {ex}");
await LoginWithGooglePlayGamesAsync();
}
}
async void LinkWithGooglePlayGamesAsync(string authCode)
{
try
{
Logger.Log("Authentication - linking with google play games");
await AuthenticationService.Instance.LinkWithGooglePlayGamesAsync(authCode);
Logger.Log("Authentication - linking with google play games successful");
OnLoginComplete?.Invoke();
}
catch (AuthenticationException ex) when (ex.ErrorCode == AuthenticationErrorCodes.AccountAlreadyLinked)
{
Logger.LogError("Authentication - account already linked, attempting to sign in with google play games");
await SignInWithGooglePlayGamesAsync(authCode);
}
catch (Exception ex)
{
Logger.LogError($"Authentication - failed to link with google play games using token: {ex}");
await SignInAnonymouslyAsync();
}
}
private async Task SignInAnonymouslyAsync()
{
Logger.Log("Authentication - attempting to sign in anonymously");
await AuthenticationService.Instance.SignInAnonymouslyAsync();
Logger.Log("Authentication - sign in anonymously successful");
OnLoginComplete?.Invoke();
}
}