The Authentication - Login With Google Account docs and Unity SDK are missing the "AccessToken" variable. I found this while trying to set up "login with Google" as an option for all of our platforms. I was going through the Setting up PlayFab authentication using Google and HTML5 tutorial to see how it is done on a platform besides Android. I couldn't get the "ServerAuthCode" variable in the PlayFabClient SDK and LoginWithGoogleAccountRequest to work even when getting the "server auth code" for offline access from this endpoint: "https://accounts.google.com/o/oauth2/v2/auth". Then I tried getting an access token from this endpoint: "https://www.googleapis.com/oauth2/v4/token". Using the access token from that endpoint I would get a response from PlayFab of "malformed code" or something along those lines. That was when I realized the request in the HTML tutorial had a different vairable called "AccessToken"! When using that variable in the request I can now authenticate successfully on any platform with Google!
Not Working Request in Unity PlayFabClientAPI:
LoginWithGoogleAccountRequest request = new LoginWithGoogleAccountRequest { TitleId = PlayFabSettings.TitleId, ServerAuthCode = authToken, InfoRequestParameters = new GetPlayerCombinedInfoRequestParams { GetPlayerProfile = true } }; PlayFabClientAPI.LoginWithGoogleAccount(request, OnLoginSucceeded, OnLoginFailed);
Workaround:
using (HttpClient client = new HttpClient()) { var jsonBodyContent = new { AccessToken = authToken, CreateAccount = true, TitleId = PlayFabSettings.TitleId }; StringContent bodyContent = new StringContent(JsonConvert.SerializeObject(jsonBodyContent), Encoding.UTF8, "application/json"); var result = await client.PostAsync($"https://{PlayFabSettings.TitleId}.playfabapi.com/Client/LoginWithGoogleAccount", bodyContent); Debug.Log(await result.Content.ReadAsStringAsync()); }
I can now authenticate all of my platforms with Google :).
The act of doing this and following the OAuth 2.0 process wasn't too challenging. I figured all of that out in a few hours. It then took me the next couple of days to figure out why I couldn't authenticate with the PlayFabClientAPI in Unity. The SDK and docs are missing the "AccessToken" variable so the current workaround is to hit the endpoint directly by creating an Http request separate from the SDK.
TL; DR;
The Unity PlayFab SDK and Authentication - Login With Google Account docs are missing the "AccessToken" variable.
Update:
The particular section in the html docs where the "AccessToken" variable is used can be found here.
Answer by Sarah Zhang · Apr 17, 2020 at 08:44 AM
Thanks for your sharing. We will inform our team about this issue of documentation content’s omission and try to reproduce this issue of Unity SDK.
Answer by TCROC · Apr 29, 2020 at 12:20 AM
Another workaround is to extend the "LoginWithGoogleAccountRequest" and add the missing argument. This is much nicer because then you can still use the SDK:
public class LoginWithGoogleAccountRequestAccessToken : LoginWithGoogleAccountRequest { public string AccessToken; }
Where "LoginWithGoogleAccountRequest" is the request included in the SDK. Then you can use the SDK like:
var loginRequest = new LoginWithGoogleAccountRequestAccessToken { AccessToken = authToken, CreateAccount = true, TitleId = PlayFabSettings.TitleId }; PlayFabClientAPI.LoginWithGoogleAccount(loginRequest, OnLoginSucceeded, OnLoginFailed);
I don't want to mark this answer as correct because it is just a workaround. I will wait until we get an official answer from PlayFab when the SDK is fixed.
Answer by Tung Thanh Nguyen · Dec 18, 2020 at 09:24 PM
@TCROC: Hi, i am new on PlayFab in Unity, can you give me some sample project for google sign-in? Thanks so much, i don't know how to get AccessToken