question

Travis Lange avatar image
Travis Lange asked

"Authentication - Login With Google Account" docs and Unity SDK are missing the "AccessToken" variable

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.

apisunity3dsdksAuthenticationdocumentation
10 |1200

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

Travis Lange avatar image
Travis Lange answered

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.

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.

Tobias Lint avatar image Tobias Lint commented ·

Thank You @TCROC. Really helped me out here. Banged my head against this for 2 days. Thanks!!

0 Likes 0 ·
seryas avatar image seryas commented ·

hi just a question, from where can i get the authToken .. is there any code example for this (not for mobile)

0 Likes 0 ·
Sarah Zhang avatar image
Sarah Zhang answered

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.

10 |1200

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

Tung Thanh Nguyen avatar image
Tung Thanh Nguyen answered

@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

10 |1200

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

seryas avatar image
seryas answered

hi just a question, from where can i get the authToken .. is there any code example for this (not for mobile)

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.