Skip to content

RequestServerSideAccess returns a null or empty authCode #3349

Open
@alandfranco

Description

@alandfranco

Hi, I'm having trouble signing in to Unity services.

When I open the app, it logs in automatically with Google Play, and I can see my username, userId, etc.
However, when I try to link the authentication service of Unity with Google Play, it fails

I checked the SHA1 code, and it's correct. I tried another code, but it didn't work at all.
I have an OAuth 2.0 web client from which I took the client ID and client secret, and an Android client with the SHA1 code.

This is the code that should do everything.
`public async void SignIn()
{
PlayGamesPlatform.Activate();

if (PlayGamesPlatform.Instance.IsAuthenticated())
{
    await UnityServices.InitializeAsync();


    Debug.Log("Google Play already authenticated. Getting auth code for Unity...");            
    PlayGamesPlatform.Instance.RequestServerSideAccess(true, async authCode =>
    {
        if (string.IsNullOrEmpty(authCode))
        {
            Debug.LogError("Failed to retrieve auth code from Google Play.");
            return;
        }

        try
        {
            await UnityServices.InitializeAsync();

            if (!AuthenticationService.Instance.IsSignedIn)
            {
                await AuthenticationService.Instance.SignInWithGooglePlayGamesAsync(authCode);
                Debug.Log("Unity sign-in complete. PlayerID: " + AuthenticationService.Instance.PlayerId);
            }
            else
            {
                await AuthenticationService.Instance.LinkWithGooglePlayGamesAsync(authCode);
            }

            RaiseLoginEvent();
        }
        catch (Exception ex)
        {
            Debug.LogError("Unity sign-in failed: " + ex.Message);
        }
    });
}
else
{
    await Authenticate();
}

}

public async Task Authenticate()
{
await UnityServices.InitializeAsync();

PlayGamesPlatform.Activate();

PlayGamesPlatform.Instance.Authenticate((status) =>
{
    if (status == SignInStatus.Success)
    {
        string name = PlayGamesPlatform.Instance.GetUserDisplayName();
        string id = PlayGamesPlatform.Instance.GetUserId();
        string imgUrl = PlayGamesPlatform.Instance.GetUserImageUrl();

        Debug.Log("Google Play Games login successful: " + name);

        // Request auth code after new login
        
        PlayGamesPlatform.Instance.RequestServerSideAccess(true, async authCode =>
        {
            if (string.IsNullOrEmpty(authCode))
            {
                Debug.LogError("Auth code is null or empty. " + authCode);
            }
            else
            {
                Debug.Log($"Auth code received: {authCode}");
            }

            try
            {
                if (!AuthenticationService.Instance.IsSignedIn)
                {
                    await AuthenticationService.Instance.SignInWithGooglePlayGamesAsync(authCode);
                    
                    Debug.Log("Unity sign-in complete. PlayerID: " + AuthenticationService.Instance.PlayerId);
                }
                else
                {
                    await AuthenticationService.Instance.LinkWithGooglePlayGamesAsync(authCode);
                }

                RaiseLoginEvent();
            }
            catch (Exception ex)
            {
                Debug.LogError("Unity sign-in failed: " + ex.Message);
            }
        });
    }
    else
    {
        DetailsText.text = "Sign in Failed!!";
        Debug.LogError("Google Play Games login failed.");
    }
});

}`

I saw multiple tutorials, I have read all the documentation, and I cannot find what is wrong with my code or project configuration.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions