question

Chris B. avatar image
Chris B. asked

Firebase, Google Login, and Unity

I am adding Google login to my Unity project (note, not the Google play sdk, just the login). I have almost everything set up and used Firebase to generate my json file for my app. I'm able to get logged in to Google and I pass the server token to Playfab using loginWithGoogle, but I get the mismatch redirect_uri error.

So I read over this https://api.playfab.com/docs/tutorials/landing-players/sign-in-with-google, but since I'm going through the Google sign in on Firebase, the interface is a little different.

I tried adding the oauth.playfab.com to the authorized domain section, but I am still getting the same error. Anybody have experience with using Firebase and setting up Google sign in through that and know what step I'm missing?

I'm not actually going through Firebase authentication, I just setup the Google Login through it and got all the stuff setup there.

Authentication
10 |1200

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

pfnathan avatar image
pfnathan answered

Unfortunately, we do not support Firebase yet, so the method which you have used will not work.

For your reference, we recommend reviewing our 2 part video tutorials which available on Youtube:

https://www.youtube.com/results?search_query=Playfab+google

2 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.

Chris B. avatar image Chris B. commented ·

Ah, ok. Well, maybe Playfab will support Firebase sometime in the near future. I was trying to avoid using Google Play Games, and the straight up Google Log in was nice since it didn't add the other stuff.

Thanks for the help.

Edit: Ok, actually got this to work with Firebase. I had to add the oauth to the Google sign in credentials. I then tried to log in and it created my account and shows it's linked to Google. So it appears to be working, unless there is something I missed somewhere. If for some reason this is incorrect on Playfabs side, please let me know, but otherwise, it seems right?

1 Like 1 ·
pfnathan avatar image pfnathan ♦ commented ·

Thanks for the update, the problem before was that the auth ticket Firebase provided did not grant the right ticket. Google may have fixed this issue recently.

Would you mind sending us your code sample so that we can validate that it is working and incorporate it into our docs?

0 Likes 0 ·
Chris B. avatar image
Chris B. answered

Sure thing! That way I know I didn’t do anything odd when setting this up.

I’m using this sdk in Unity 2017.2. https://github.com/googlesamples/google-signin-unity

I then went to Firebase and created a new project. Under authentication -> sign in, you can add Google as an option. Where you enable it, there is a section for Web SDK configuration. This is where I got the web client ID and the Web client secret.

Also on this screen is a section that says Whitelist client IDs from external projects. There is a question mark next to it that includes a link to the Google API Console. Going there I get taken to the credentials.

Under there is a section for OAuth 2.0 client IDs. The web one should be listed there (and Android one if you set that up). There is a pencil to edit this.

This part I hope is correct. There is an Authorized redirect URIs in that section. I added https://oauth.playfab.com/oauth2/google to there.

Also need to get the sha1 key, which this explains how https://developers.google.com/android/guides/client-auth

This is used for the Android setup and will require your keystore from the Android build.

Once all that was setup, I was able to get the google-services.json file on Firebase. This included everything I needed and I added it to my Unity project.

The login code I used below.

private GoogleSignInConfiguration gConfig;

private void Awake()
{
//googleWebId is the web client ID from above
   gConfig = new GoogleSignInConfiguration
   {
      WebClientId = googleWebId,
      RequestIdToken = true
   };
}

public void LoginWithGoogle()
{
   GoogleSignIn.Configuration = gConfig;
   GoogleSignIn.Configuration.UseGameSignIn = false;
   GoogleSignIn.Configuration.RequestIdToken = true;
   GoogleSignIn.Configuration.RequestAuthCode = true;
        
   Debug.Log("Google Sign In");

   GoogleSignIn.DefaultInstance.SignIn().ContinueWith(GoogleSignInComplete);
}

internal void GoogleSignInComplete(Task<GoogleSignInUser> task)
{
   if (task.IsFaulted)
   {
      using (IEnumerator<System.Exception> enumerator =
                  task.Exception.InnerExceptions.GetEnumerator())
      {
         if (enumerator.MoveNext())
         {
            GoogleSignIn.SignInException error = (GoogleSignIn.SignInException)enumerator.Current;
            Debug.Log("Got Error: " + error.Status + " " + error.Message);
         }
         else
         {
            Debug.Log("Got Unexpected Exception?!?" + task.Exception);
         }
      }
   }
   else if (task.IsCanceled)
   {
      Debug.Log("Canceled");
   }
   else
   {
      Debug.Log("IDTOKEN " + task.Result.IdToken);
      Debug.Log("AUTHCODE " + task.Result.AuthCode);
      Debug.Log("Welcome: " + task.Result.DisplayName + "!");
      PlayFabClientAPI.LoginWithGoogleAccount(new LoginWithGoogleAccountRequest
      {
         ServerAuthCode = task.Result.AuthCode,
         CreateAccount = true,
         TitleId = playFabGameID

      }, (result) =>
      {
         print("PlayfabResults " + result.PlayFabId);
      }, (error) =>
      {
         print("ERROR " + error.ErrorMessage + " :::: " + error.ErrorDetails);
      });      
   }
}

1 comment
10 |1200

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

pfnathan avatar image pfnathan ♦ commented ·

Thank you for sharing sample code.

0 Likes 0 ·
sauliusvince avatar image
sauliusvince answered

Hello, I'm been trying for a week to do the same thing. Login via firebase and then when I extract AuthCode, I want to connect with google to the Playfab. Google side works, ask for email selection and retrieves server auth. However, when I try to login with playfab I get error: "unauthorized_client details".

Don't remember what I did, but everything is working.

10 |1200

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

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.