question

Batuhan Bozkan avatar image
Batuhan Bozkan asked

Photon + Playfab Authentication

I implemented Playfab + Photon Auth system as Playfab tutorial described. But these couple of days I have been getting "OperationResponse 230: ReturnCode: 32755 (Invalid token). Parameters: {} Server: NameServer Address: ns.exitgames.com:5058" error.

Here is my code snippet

private void PhotonAuth()
    {

        PlayFabClientAPI.GetPhotonAuthenticationToken(new GetPhotonAuthenticationTokenRequest()
        {
            PhotonApplicationId = PhotonNetwork.PhotonServerSettings.AppSettings.AppIdRealtime
        }, AuthenticateWithPhoton, OnPlayfabError); ;
        
    }

    private void AuthenticateWithPhoton(GetPhotonAuthenticationTokenResult result)
    {

        Debug.Log("Photon token for " + playfabID + " acquired: " + result.PhotonCustomAuthenticationToken);

        AuthenticationValues customAuth = new AuthenticationValues
        {
            AuthType = CustomAuthenticationType.Custom
        };

        customAuth.AddAuthParameter("username", playfabID);     // expected by PlayFab custom auth service

        customAuth.AddAuthParameter ("token", result.PhotonCustomAuthenticationToken);

        PhotonNetwork.AuthValues = customAuth;

        PhotonNetwork.ConnectUsingSettings();

    }

Is there anything I am missing?

Thanks in advance
photonAuthentication
10 |1200

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

Sarah Zhang avatar image
Sarah Zhang answered

We can’t reproduce this error in our testing title. We followed our tutorial -- https://docs.microsoft.com/en-us/gaming/playfab/sdks/photon/quickstart, test the sample code with the PUN2 example project. It works fine. You can refer to the following testing code.

Do you mean you ever succeeded to implement the Photon Auth system with PlayFab but received this error recently? If so, could you please tell us if you changed the Photon app’s setting in the Photon dashboard? Besides, could you please provide more detailed error messages? For example, have you received some errors from PlayFab?

using Photon.Pun;
using Photon.Realtime;
using PlayFab;
using PlayFab.ClientModels;
using UnityEngine;


public class PlayFabAuthenticator : MonoBehaviour
{


    private string _playFabPlayerIdCache;


    //Run the entire thing on awake
    public void Awake()
    {


        AuthenticateWithPlayFab();


    }


    /*
     * Step 1
     * We authenticate current PlayFab user normally.
     * In this case we use LoginWithCustomID API call for simplicity.
     * You can absolutely use any Login method you want.
     * We use PlayFabSettings.DeviceUniqueIdentifier as our custom ID.
     * We pass RequestPhotonToken as a callback to be our next step, if
     * authentication was successful.
     */
    private void AuthenticateWithPlayFab()
    {
        LogMessage("PlayFab authenticating using Custom ID...");


        PlayFabClientAPI.LoginWithCustomID(new LoginWithCustomIDRequest()
        {
            CreateAccount = true,
            CustomId = PlayFabSettings.DeviceUniqueIdentifier
        }, RequestPhotonToken, OnPlayFabError);
    }


    /*
    * Step 2
    * We request Photon authentication token from PlayFab.
    * This is a crucial step, because Photon uses different authentication tokens
    * than PlayFab. Thus, you cannot directly use PlayFab SessionTicket and
    * you need to explicitly request a token. This API call requires you to
    * pass Photon App ID. App ID may be hard coded, but, in this example,
    * We are accessing it using convenient static field on PhotonNetwork class
    * We pass in AuthenticateWithPhoton as a callback to be our next step, if
    * we have acquired token successfully
    */
    private void RequestPhotonToken(LoginResult obj)
    {
        LogMessage("PlayFab authenticated. Requesting photon token...");
        //We can player PlayFabId. This will come in handy during next step
        _playFabPlayerIdCache = obj.PlayFabId;


        PlayFabClientAPI.GetPhotonAuthenticationToken(new GetPhotonAuthenticationTokenRequest()
        {
            PhotonApplicationId = PhotonNetwork.PhotonServerSettings.AppSettings.AppIdRealtime
        }, AuthenticateWithPhoton, OnPlayFabError);
    }


    /*
     * Step 3
     * This is the final and the simplest step. We create new AuthenticationValues instance.
     * This class describes how to authenticate a players inside Photon environment.
     */
    private void AuthenticateWithPhoton(GetPhotonAuthenticationTokenResult obj)
    {
        LogMessage("Photon token acquired: " + obj.PhotonCustomAuthenticationToken + "  Authentication complete.");


        //We set AuthType to custom, meaning we bring our own, PlayFab authentication procedure.
        var customAuth = new AuthenticationValues { AuthType = CustomAuthenticationType.Custom };
        //We add "username" parameter. Do not let it confuse you: PlayFab is expecting this parameter to contain player PlayFab ID (!) and not username.
        customAuth.AddAuthParameter("username", _playFabPlayerIdCache);    // expected by PlayFab custom auth service


        //We add "token" parameter. PlayFab expects it to contain Photon Authentication Token issues to your during previous step.
        customAuth.AddAuthParameter("token", "");


        //We finally tell Photon to use this authentication parameters throughout the entire application.
        PhotonNetwork.AuthValues = customAuth;
        Debug.Log(PhotonNetwork.ConnectUsingSettings().ToString());


    }


    private void OnPlayFabError(PlayFabError obj)
    {
        LogMessage(obj.GenerateErrorReport());
    }


    public void LogMessage(string message)
    {
        Debug.Log("PlayFab + Photon Example: " + message);
    }
}
10 |1200

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

Akash avatar image
Akash answered

Thank you so much for the answer, for me it was the "AuthType".

Setting the AuthType is the MOST IMPORTANT part of it.

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.