question

Darius Vu avatar image
Darius Vu asked

How to login automatically with Google Play Games?

Dear support team,

I tried to implement the login with Google Play Games as the tutorial in here:

https://docs.microsoft.com/en-us/gaming/playfab/features/authentication/platform-specific-authentication/google-sign-in-unity

It is working well for the first time when an user logins the app. And I want to develop the automatically login when the user opens the app in the next time and don't need to accept the condition.

In order to do like this, I stored the access token using PlayerPrefs in Unity and when the user opens the app then the Login() function is called and uses the access token to login.

In details, I stored string accessToken = AccessToken.CurrentAccessToken.TokenString; with Facebook Login while string serverAuthCode = PlayGamesPlatform.Instance.GetServerAuthCode(); is saved in Google Login. You can see the source below as the reference.

However, it is only working well with Facebook login method and it had the issue with Google login. When the user opens the app in the next time, the Login() function is called then the error - InvalidGoogleToken happened. I guess that serverAuthCode is changed and invalid. So do you have any solution for this issue?

Thank you so much for your support!!!

//Login with Facebook
string accessToken = AccessToken.CurrentAccessToken.TokenString;
string userID = AccessToken.CurrentAccessToken.UserId;
// Store the AccessToken in PlayerPrefs 
PlayfabManager.PlayFab.account.SetFacebookToken(userID, accessToken);
// Call LoginWithFacebook() Playfab API
PlayfabManager.PlayFab.LoginWithFacebook(accessToken, AfterLoginAction, errorCallbackFacebookLogin);
 // Login with Google Play Games
string serverAuthCode = PlayGamesPlatform.Instance.GetServerAuthCode();
string email = PlayGamesPlatform.Instance.GetUserEmail();
// Store the serverAuthCode in PlayerPrefs 
PlayfabManager.PlayFab.account.SetGoogleToken(email, serverAuthCode);
// Call LoginWithGoogleAccount() Playfab API
PlayfabManager.PlayFab.LoginWithGoogle(serverAuthCode, AfterLoginAction, errorCallbackGoogleLogin);         
// For automatically login 

public void Login(Action<LoginResult> successCallback, Action<PlayFabError> errorCallback)
{
	switch (account.memberType)
	{
	case EMemberType.Google:
		LoginWithGoogle(account.GoogleToken, successCallback, errorCallback);
		break;

	case EMemberType.Facebook:
		LoginWithFacebook(account.FacebookToken, successCallback, errorCallbac	k);
		break;
	}
}
apis
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Citrus Yan avatar image
Citrus Yan answered

Attempting to re-use the Server Auth Code in the call to LoginWithGoogleAccount is likely to resultin an error, for more details please check out this thread:

https://community.playfab.com/questions/11804/authenticate-with-googleplay-services-login-works.html

In your case, you should ask the player to re-authenticate with google if the Server Auth Code is invalid.

5 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Darius Vu avatar image Darius Vu commented ·

Hi Citrus Yan,

Re-authenticate is not good solution, I think. Because each time when the user opens the app then I see that Server Auth Code is changed and invalid -> ask the player to re-authenticate with google. In this way, the user is always asked to re-authenticate -> it is not good solution. Do you have any solution to re-use the Server Auth Code or re-authenticate automatically without the action from the player?

0 Likes 0 ·
Citrus Yan avatar image Citrus Yan Darius Vu commented ·

[Edited]

You can consider implementing silent login, for more info, please see discussions in this thread:

https://community.playfab.com/questions/21206/authentication-via-google-play-games.html?childToView=21549#comment-21549

0 Likes 0 ·
Darius Vu avatar image Darius Vu Citrus Yan commented ·

Hi Citrus Yan,

The link is maybe wrong, could you put the correct link to help me?

0 Likes 0 ·
Show more comments
Show more comments

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.