Search code examples
unity-game-enginegoogle-apigoogle-oauthgoogle-signinplayfab

Google Signin for Unity (Windows standalone build) - invalidGoogleToken when used with Playfab


Hopefully someone can point me in the right direction here, I'm attempting to log into Playfab using a google auth code retrieved from an async call in Unity, the flow is as follows:

  1. Click my login to google button in Unity.

  2. Unity begins to listen for the response and opens a google login browser session.

  3. User clicks email/signs into email they want.

  4. This response is returned to Unity with an auth code.

  5. I have then attempted to use the returned google auth code with playfabs LoginWithGoogleAccount method:

PlayFabClientAPI.LoginWithGoogleAccount(new LoginWithGoogleAccountRequest()
{
    TitleId = PlayFabSettings.TitleId,
    ServerAuthCode = returnedWindowsGoogleAuthCode,
     
    CreateAccount = true,
}, OnPlayfabGooglePlayAuthComplete, OnPlayfabGooglePlayAuthFailed);
  1. This then fails with a returned response from playfab with "invalidGoogleToken".

From what I've read from what I've come across on google is that this token is possibly in a "used" state by the time I am calling LoginWithGoogleAccount and I possibly need a refreshed token? but I am not too familiar with the Google API so I could be completely off the mark there? But if this is the case what should I do here? How do I re-request a valid token without going back to the browser to do the same thing again?

To give you an idea of pretty much the exact code I'm looking at but I've altered slightly to get it to work in Unity I am following the Google Sample OAuthDesktopApp code: OAuthDesktopApp Sample Code

I am calling the method "button_Click", this runs and makes the request, Unity then begins listening for the google response at line 72 and the auth code is then output to logs at line 129.

This auth code output at line 129 is what I have then been passing back in to LoginWithGoogleAccount which then results in the failed "invalidGoogleToken", this does also then run a request for user information which does correctly return the name/email of the user you signed in with etc.

Just to add to my previous comments, if I remove the call to performCodeExchange line 132 (I read somewhere that at that point I am swapping the auth code for a token and thus the auth code will no longer work? I'm guessing here) and then if I call PlayFabClientAPI.LoginWithGoogleAccount with the auth code I am presented with:

PlayFabError error:

error.GetHashCode(): 2051826304

error.Error: InvalidGoogleToken

error.ErrorMessage: invalid_grant details: Missing code verifier.

error.ErrorDetails: null

Hopefully this may help someone guide me.


Solution

  • First thanks to those that replied to this, So I've managed to solve this after a comment from a Playfab mod, There appears to be no official way from Google to login using a standlone Unity build nor an official way to then login to Playfab using the same build method.

    The solution I've used uses a modified Unity version of one of the Google samples from OAuth 2.0 for Mobile & Desktop Apps . This will allow you to get the oauth code within Unity then you can pass this oauth code to Playfab for login, BUT Playfab has since deprecated the way to do this (which is silly if its the only solution) so you need to modifiy Playfabs SDK to include the accesstoken then you can use this to finally login.