Separate or single authentication for photon realtime and photon chat?

Hello,

I would like to ask do need to separately authenticate photon realtime and photon chat or both are done using this:
private void RequestPhotonToken(PFLoginResult result)
    {
        Debug.Log("PlayFab authenticated. Requesting photon token...");

        _playFabPlayerId = result.PlayFabId;

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

    private void AuthenticateWithPhoton(GetPhotonAuthenticationTokenResult obj)
    {
        Debug.Log("Photon token acquired: " + obj.PhotonCustomAuthenticationToken + "  Authentication complete.");

        var customAuth = new AuthenticationValues { AuthType = CustomAuthenticationType.Custom };
        
        customAuth.AddAuthParameter("username", _playFabPlayerId);    // expected by PlayFab custom auth service       

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

        PhotonNetwork.AuthValues = customAuth;

    }
Also if needs separate authentication please guide me through that.

I would like to ask one more thing, since I have created another application for chat in photon dashboard, do I have to set custom server authentication for chat application too or only setting in photon realtime application is enough.

Comments

  • Hi @avimon,

    If you want to use Chat and PUN you need two separate applications, one per type.
    If you want to make use of PlayFab authentication for both PUN and Chat, you need to authenticate each one separately.
    Follow the guide for each application type:

    - PUN
    - Chat

    These pages contain the steps required and the client code.
  • @JohnTube Do I have to set custom server in Chat app also which I created in my Photon dashboard just like I did for realtime app?
  • yes.
  • @JohnTube what is chatClient in code snippet given in photon guide for Chat authenticatio?
  • object of type ChatClient.
    Class and file can be found in "Assets\PhotonChatApi" available in PUN or in Photon .Net SDKs (Unity etc.).
  • @JohnTube Will the same photon token be returned for both realtime and chat app?
  • No get a token for each app.
  • @JohnTube Should photon have to be connected to cloud before requesting token or it is not necessary? Sorry for asking so many questions. Since I am a newbie with PlayFab I come across so many doubts.
  • No. Connect to PlayFab, get Photon token then connect to Photon Cloud.
  • @JohnTube Is that applicable for both chat and realtime apps? because I have read somewhere that its necessary for a player to join room for message transmission which is only possible after connection with photon.
    Please also explain me whether I should use PhotonNetwork.ConnectUsingSettings("") before
    ExitGames.Client.Photon.Chat.ChatClient.Connect	(	string 	appId,
    string 	appVersion,
    AuthenticationValues 	authValues 
    )
    or both of these connections are independent of each other? and really thanks a lot for clearing so many doubts.
  • both of these connections are independent of each other?
    This is correct.
    You're welcome.
  • @JohnTube Thanks a lot
  • @JohnTube I am connecting to photon cloud in a new scene in unity other than in which I have retrieved photon tokens. Will there be any problem with connection or token are just for authentication purpose?
  • Will there be any problem with connection or token are just for authentication purpose?
    There won't be any problem with the connection as tokens are just for authentication.
  • avimonavimon
    edited July 6
    @JohnTube
    using PhotonChatClient = ExitGames.Client.Photon.Chat.ChatClient;
    using PhotonChat = ExitGames.Client.Photon.Chat;
    using ExitGames.Client.Photon;
    
    private static readonly PhotonChat.IChatClientListener listener;
    public PhotonChatClient chatClient = new PhotonChatClient(listener, ConnectionProtocol.Udp);
    
    private void OnLoginSuccess(PFLoginResult result) {
         
            RequestPhotonRealtimeToken(result);
            RequestPhotonChatToken(result);
            
        }
    
    private void RequestPhotonRealtimeToken(PFLoginResult result) {
            Debug.Log("PlayFab authenticated. Requesting realtime photon token...");
    
            _playFabPlayerId = result.PlayFabId;
    
            PlayFabClientAPI.GetPhotonAuthenticationToken(new GetPhotonAuthenticationTokenRequest()
            {
                PhotonApplicationId = PhotonNetwork.PhotonServerSettings.AppID
            }, AuthenticateWithRealtimePhoton, OnPlayFabError);
        }
    
        private void AuthenticateWithRealtimePhoton(GetPhotonAuthenticationTokenResult obj) {
            Debug.Log("Realtime Photon token acquired: " + obj.PhotonCustomAuthenticationToken + "  Authentication complete.");
    
            var customAuth = new AuthenticationValues { AuthType = CustomAuthenticationType.Custom };
            customAuth.AddAuthParameter("username", _playFabPlayerId);    
            customAuth.AddAuthParameter("token", obj.PhotonCustomAuthenticationToken);
            
            PhotonNetwork.AuthValues = customAuth;
        }
    
        private void RequestPhotonChatToken(PFLoginResult result)
        {
            Debug.Log("PlayFab authenticated. Requesting chat photon token...");
    
            PlayFabClientAPI.GetPhotonAuthenticationToken(new GetPhotonAuthenticationTokenRequest()
            {
                PhotonApplicationId = PhotonNetwork.PhotonServerSettings.ChatAppID
            }, AuthenticateWithChatPhoton, OnPlayFabError);
        }
    
        private void AuthenticateWithChatPhoton(GetPhotonAuthenticationTokenResult result)
        {
            Debug.Log("Chat Photon token acquired: " + result.PhotonCustomAuthenticationToken + "  Authentication complete.");
    
            chatClient.AuthValues = new PhotonChat.AuthenticationValues();
            chatClient.AuthValues.AuthType = PhotonChat.CustomAuthenticationType.Custom;
            chatClient.AuthValues.AddAuthParameter("username", _playFabPlayerId);
            chatClient.AuthValues.AddAuthParameter("token", result.PhotonCustomAuthenticationToken);
            chatClient.AuthValues.UserId = _playFabPlayerId;
    
        }
    There is something wrong with this code here which I have implemented. Photon Realtime app is authenticated successfully and I have even recieved its JSON response in playfab but no JSON response is returned for Chat app. Does that mean my Chat app is not yet authenticated.

    Please check my chatClient object. I don't think I don't think I have correctly created it. Also help me to correct it.

    In AuthenticateWithChatPhoton method I had to write chatClient.AuthValues = new PhotonChat.AuthenticationValues(); instead of chatClient.AuthValues = new AuthenticationValues(); which is given in the photon doc because it prompted an error telling me to explicitly convert its type to ExitGames.Client.Photon.Chat May be it is because of wrong creation of my chatClient object. Please help me through this .
  • @JohnTube
    using PhotonChatClient = ExitGames.Client.Photon.Chat.ChatClient;
    using PhotonChat = ExitGames.Client.Photon.Chat;
    using ExitGames.Client.Photon;
    
    private static readonly PhotonChat.IChatClientListener listener;
    public PhotonChatClient chatClient = new PhotonChatClient(listener, ConnectionProtocol.Udp);
    
    private void OnLoginSuccess(PFLoginResult result) {
            
            RequestPhotonRealtimeToken(result);
            RequestPhotonChatToken(result);
            
    	}
    
    private void RequestPhotonRealtimeToken(PFLoginResult result) {
            Debug.Log("PlayFab authenticated. Requesting realtime photon token...");
    
            _playFabPlayerId = result.PlayFabId;
    
            PlayFabClientAPI.GetPhotonAuthenticationToken(new GetPhotonAuthenticationTokenRequest()
            {
                PhotonApplicationId = PhotonNetwork.PhotonServerSettings.AppID
            }, AuthenticateWithRealtimePhoton, OnPlayFabError);
        }
    
        private void AuthenticateWithRealtimePhoton(GetPhotonAuthenticationTokenResult obj) {
            Debug.Log("Realtime Photon token acquired: " + obj.PhotonCustomAuthenticationToken + "  Authentication complete.");
    
            var customAuth = new AuthenticationValues { AuthType = CustomAuthenticationType.Custom };
            customAuth.AddAuthParameter("username", _playFabPlayerId);    
            customAuth.AddAuthParameter("token", obj.PhotonCustomAuthenticationToken);
            
            PhotonNetwork.AuthValues = customAuth;
        }
    
        private void RequestPhotonChatToken(PFLoginResult result)
        {
            Debug.Log("PlayFab authenticated. Requesting chat photon token...");
    
            PlayFabClientAPI.GetPhotonAuthenticationToken(new GetPhotonAuthenticationTokenRequest()
            {
                PhotonApplicationId = PhotonNetwork.PhotonServerSettings.ChatAppID
            }, AuthenticateWithChatPhoton, OnPlayFabError);
        }
    
        private void AuthenticateWithChatPhoton(GetPhotonAuthenticationTokenResult result)
        {
            Debug.Log("Chat Photon token acquired: " + result.PhotonCustomAuthenticationToken + "  Authentication complete.");
    
            chatClient.AuthValues = new PhotonChat.AuthenticationValues();
            chatClient.AuthValues.AuthType = PhotonChat.CustomAuthenticationType.Custom;
            chatClient.AuthValues.AddAuthParameter("username", _playFabPlayerId);
            chatClient.AuthValues.AddAuthParameter("token", result.PhotonCustomAuthenticationToken);
            chatClient.AuthValues.UserId = _playFabPlayerId;
    
        }
    
    There is something wrong with this code here because my Photon Realtime app is successfully authenticated and I even recieved JSON response for that in PlayFab but there is no JSON response returned for Chat app. Does that mean My Chat app is not yet authenticated?

    Please check my chatClient object. I think I have created it in wrong way and if yes please help me to correct it.

    In AuthenticateWithChatPhoton method I had to write chatClient.AuthValues = new PhotonChat.AuthenticationValues(); instead of chatClient.AuthValues = new AuthenticationValues(); which was given in photon docs beacause it prompted an error telling me to explcitly convert its type to ExitGames.Client.Photon.Chat May be it is beacause of my wrong creation of chatClient object. Please guide me through this.
  • @JohnTube Do I have to download Photon Chat from Asset store or PUN is enough for implementing Photon Chat since PUN asset contains PhotonChatApi named folder in its package?
  • I even recieved JSON response for that in PlayFab but there is no JSON response returned for Chat app. Does that mean My Chat app is not yet authenticated?
    What do you mean by this? what are you referring to with "JSON response"? you mean
    GetPhotonAuthenticationTokenResult
    ? and why is it received for PUN only?

    You need to implement the Photon Chat listener interface in some class or some MonoBehaviour or even in the same class as the one you create the PhotonChat object.
    public class SomeClass : PhotonChat.IChatClientListener 
    {
    // TODO: implement interface
    You can create the PhotonChat object in a Start method of a MonoBehaviour or a constructor of another class.

    Do I have to download Photon Chat from Asset store or PUN is enough for implementing Photon Chat since PUN asset contains PhotonChatApi named folder in its package?
    PUN includes Photon Chat so no need to download anything else to use Photon Chat.
  • avimonavimon
    edited July 9
    @JohnTube Thanks a lot. Finally I got my both applications authenticated and the responses I was talking about which I got on authentication from PlayFab are as follows:

    -- img deleted as it contained an appid --

    -- img deleted as it contained an appid --

    Thanks again for guiding me whole time. Have a good day !!! :)
Sign In or Register to comment.