Search Unity

Third Party Photon Pun Unity - Bad Request

Discussion in 'Multiplayer' started by Akaton97, Oct 16, 2022.

  1. Akaton97

    Akaton97

    Joined:
    Jun 26, 2017
    Posts:
    6
    Goodmorning/evening,

    I am currently using Photon Pun for implementing Facebook and Epic external authentication, besides the room-multiplayer managment, but i am encountering some problem related to Photon Server Response. It seems that Photon connect to server but it responds with a bad request error:

    "Error authenticating to Photon using Facebook: The remote server returned an error: (400) Bad Request."

    "OperationResponse 230: ReturnCode: 32755 (The remote server returned an error: (400) Bad Request.). Parameters: {} Server: NameServer Address: ns.photonengine.io:5058"

    For the sake of simplicity, i have only worked with facebook and it seems that appId is fine, Access Token is fine, client token is fine and also the client secret. I can't understand what's going on, so i'll paste the entire authentication code here. I apology for the mess, it is still a work in progress:

    Code (CSharp):
    1. using System.Collections;
    2.  
    3. using Epic.OnlineServices;
    4.  
    5. using Epic.OnlineServices.Auth;
    6.  
    7. using Epic.OnlineServices.Logging;
    8.  
    9. using Epic.OnlineServices.Platform;
    10.  
    11. using System.Collections.Generic;
    12.  
    13. using System;
    14.  
    15. using System.Runtime.InteropServices;
    16.  
    17. using UnityEngine;
    18.  
    19. using UnityEngine.UI;
    20.  
    21. using Photon.Pun;
    22.  
    23. using Photon.Realtime;
    24.  
    25. using TMPro;
    26.  
    27. using Facebook.Unity; //import facebook sdk
    28.  
    29. public class AuthenticationManager : MonoBehaviourPunCallbacks
    30.  
    31. {
    32.  
    33.     #region EPIC ONLINE SERVICE VARIABLES
    34.  
    35.     public string m_ProductName = "MyUnityApplication";
    36.  
    37.     public string m_ProductVersion = "1.0";
    38.  
    39.     public string m_ProductId = "";
    40.  
    41.     public string m_SandboxId = "";
    42.  
    43.     public string m_DeploymentId = "";
    44.  
    45.     public string m_ClientId = "";
    46.  
    47.     public string m_ClientSecret = "";
    48.  
    49.     public LoginCredentialType m_LoginCredentialType = LoginCredentialType.AccountPortal;
    50.  
    51.     // These fields correspond to \<see cref="Credentials.Id" /> and \<see cref="Credentials.Token" />,
    52.  
    53.     // and their use differs based on the login type. For more information, see \<see cref="Credentials" />
    54.  
    55.     // and the Auth Interface documentation.
    56.  
    57.     public string m_LoginCredentialId = null;
    58.  
    59.     public string m_LoginCredentialToken = null;
    60.  
    61.  
    62.     private static PlatformInterface s_PlatformInterface;
    63.  
    64.     private const float c_PlatformTickInterval = 0.1f;
    65.  
    66.     private float m_PlatformTickTimer = 0f;
    67.  
    68.     private bool showConnectionStatus = false;
    69.  
    70.  
    71.  
    72.     InitializeOptions initializeOptions;
    73.  
    74.     Result initializeResult;
    75.  
    76.     Options options;
    77.  
    78.     LoginOptions loginOptions;
    79.  
    80.     #endregion
    81.  
    82.  
    83.     [Header("Profile Interface Objects")]
    84.  
    85.     public GameObject profilePicture_obj;
    86.  
    87.     public GameObject profileNickname_obj;
    88.  
    89.     public GameObject profileEmail_obj;
    90.  
    91.  
    92.     [Header("UI interfaces")]
    93.  
    94.     public GameObject uI_Profile;
    95.  
    96.     public GameObject uI_connectionStatus;
    97.  
    98.     public GameObject uI_Authentication;
    99.  
    100.     public GameObject anonAuth_Panel;
    101.  
    102.     public GameObject profileBackgroundIcon_obj;
    103.  
    104.     public TMP_InputField playerNameInputField;
    105.  
    106.     private static Sprite defaultProfileIcon;
    107.  
    108.     public TMP_Text connectionStatusText;
    109.  
    110.     void Start()
    111.  
    112.     {
    113.  
    114.         defaultProfileIcon = profileBackgroundIcon_obj.GetComponent<Sprite>();
    115.  
    116.         initializeOptions = new InitializeOptions()
    117.  
    118.         {
    119.  
    120.             ProductName = m_ProductName,
    121.  
    122.             ProductVersion = m_ProductVersion
    123.  
    124.         };
    125.  
    126.  
    127.         initializeResult = PlatformInterface.Initialize(ref initializeOptions);
    128.  
    129.         if (initializeResult != Result.Success)
    130.  
    131.         {
    132.  
    133.             #if UNITY_EDITOR
    134.  
    135.             Debug.Log("Failed to initialize platform: " + initializeResult);
    136.  
    137.             #else
    138.  
    139.             throw new Exception("Failed to initialize platform: " + initializeResult);
    140.  
    141.             #endif
    142.  
    143.         }
    144.  
    145.  
    146.         // The SDK outputs lots of information that is useful for debugging.
    147.  
    148.         // Make sure to set up the logging interface as early as possible: after initializing.
    149.  
    150.         LoggingInterface.SetLogLevel(LogCategory.AllCategories, LogLevel.VeryVerbose);
    151.  
    152.         LoggingInterface.SetCallback((ref LogMessage logMessage) => Debug.Log(logMessage.Message));
    153.  
    154.  
    155.         options = new Options()
    156.  
    157.         {
    158.  
    159.             ProductId = m_ProductId,
    160.  
    161.             SandboxId = m_SandboxId,
    162.  
    163.             DeploymentId = m_DeploymentId,
    164.  
    165.             ClientCredentials = new ClientCredentials()
    166.  
    167.             {
    168.  
    169.                 ClientId = m_ClientId,
    170.  
    171.                 ClientSecret = m_ClientSecret
    172.  
    173.             }
    174.  
    175.         };    
    176.  
    177.     }
    178.  
    179.    
    180.  
    181.     public static void logOutFromAuth()
    182.  
    183.     {
    184.  
    185.         if(FB.IsLoggedIn)
    186.  
    187.         {
    188.  
    189.             FB.LogOut();
    190.  
    191.             PhotonNetwork.Disconnect();
    192.  
    193.             //GameObject.Find("Profile_Icon").GetComponent<Image>().sprite = defaultProfileIcon;
    194.  
    195.         }
    196.  
    197.     }
    198.  
    199.  
    200.     private void Update()
    201.  
    202.     {
    203.  
    204.         if (s_PlatformInterface != null)
    205.  
    206.         {
    207.  
    208.             m_PlatformTickTimer += Time.deltaTime;
    209.  
    210.  
    211.             if (m_PlatformTickTimer >= c_PlatformTickInterval)
    212.  
    213.             {
    214.  
    215.                 m_PlatformTickTimer = 0;
    216.  
    217.                 s_PlatformInterface.Tick();
    218.  
    219.             }
    220.  
    221.         }
    222.  
    223.         if(showConnectionStatus)
    224.  
    225.         {
    226.  
    227.             connectionStatusText.text = "CONNECTION STATUS: " + PhotonNetwork.NetworkClientState;
    228.  
    229.         }
    230.  
    231.     }
    232.  
    233.  
    234.     private void Awake()  
    235.  
    236.     {
    237.  
    238.          if (!FB.IsInitialized)
    239.  
    240.         {
    241.  
    242.             Debug.Log("FB is initializing");
    243.  
    244.             // Initialize the Facebook SDK
    245.  
    246.             FB.Init();
    247.  
    248.         }
    249.  
    250.     }
    251.  
    252.  
    253.     #region "UI manager"
    254.  
    255.     public void OnFacebookAuthButtonClicked()
    256.  
    257.     {
    258.  
    259.         if (FB.IsLoggedIn)
    260.  
    261.         {
    262.  
    263.             OnFacebookLoggedIn();
    264.  
    265.         }
    266.  
    267.         else
    268.  
    269.         {
    270.  
    271.             var perms = new List<string>(){"public_profile", "email"};
    272.  
    273.             FB.LogInWithReadPermissions(perms, FBAuthCallback);
    274.  
    275.         }
    276.  
    277.     }
    278.  
    279.  
    280.     public void OnEpicAuthButtonClicked()
    281.  
    282.     {
    283.  
    284.         if (!PhotonNetwork.IsConnected)
    285.  
    286.         {
    287.  
    288.             EpicLogin();
    289.  
    290.         }
    291.  
    292.     }
    293.  
    294.  
    295.     public void OnAnonymousAuthButtonClicked()
    296.  
    297.     {
    298.  
    299.         if(!PhotonNetwork.IsConnected)
    300.  
    301.         {
    302.  
    303.             photonAnonymousLogin();
    304.  
    305.         }
    306.  
    307.     }
    308.  
    309.     #endregion
    310.  
    311.  
    312.     #region "FACEBOOK callbacks"
    313.  
    314.     private void FBAuthCallback(ILoginResult result)
    315.  
    316.     {
    317.  
    318.         if (FB.IsLoggedIn)
    319.  
    320.         {
    321.  
    322.             OnFacebookLoggedIn();
    323.  
    324.         }
    325.  
    326.         else
    327.  
    328.         {
    329.  
    330.             Debug.LogErrorFormat("Error in Facebook login {0}", result.Error);
    331.  
    332.         }
    333.  
    334.     }
    335.  
    336.  
    337.     private void OnFacebookLoggedIn()
    338.  
    339.     {
    340.  
    341.         // AccessToken class will have session details
    342.  
    343.         string aToken = AccessToken.CurrentAccessToken.TokenString;
    344.  
    345.         Debug.Log("ACCESS TOKEN FB: "+aToken );
    346.  
    347.         string facebookId = AccessToken.CurrentAccessToken.UserId;;
    348.  
    349.         PhotonNetwork.AuthValues = new AuthenticationValues();
    350.  
    351.         //use auth "Facebook" becaus the app permission asked are for fb application
    352.  
    353.         PhotonNetwork.AuthValues.AuthType = CustomAuthenticationType.Facebook;
    354.  
    355.         PhotonNetwork.AuthValues.UserId = facebookId; // alternatively set by server
    356.  
    357.         PhotonNetwork.AuthValues.AddAuthParameter("token", aToken);
    358.  
    359.         PhotonNetwork.ConnectUsingSettings();
    360.  
    361.         OnConnectedToMaster();
    362.  
    363.     }
    364.  
    365.     #endregion
    366.  
    367.  
    368.     #region Epic Games Callback
    369.  
    370.     private void EpicLogin()
    371.  
    372.     {
    373.  
    374.         s_PlatformInterface = PlatformInterface.Create(ref options);
    375.  
    376.         if (s_PlatformInterface == null)
    377.  
    378.         {
    379.  
    380.             throw new Exception("Failed to create platform");
    381.  
    382.         }
    383.  
    384.         loginOptions = new LoginOptions()
    385.  
    386.         {
    387.  
    388.             Credentials = new Credentials()
    389.  
    390.             {
    391.  
    392.                 Type = m_LoginCredentialType,
    393.  
    394.                 Id = m_LoginCredentialId,
    395.  
    396.                 Token = m_LoginCredentialToken
    397.  
    398.             }
    399.  
    400.         };
    401.  
    402.      
    403.  
    404.         // Ensure platform tick is called on an interval, or this will not callback.
    405.  
    406.         s_PlatformInterface.GetAuthInterface().Login(ref loginOptions, null, (ref LoginCallbackInfo loginCallbackInfo) =>
    407.  
    408.         {
    409.  
    410.             if (loginCallbackInfo.ResultCode == Result.Success)
    411.  
    412.             {
    413.  
    414.                 Debug.Log("Login succeeded");              
    415.  
    416.                 PhotonNetwork.AuthValues = new AuthenticationValues();
    417.  
    418.                 //use auth "Epic" becaus the app permission asked are for epic games
    419.  
    420.                 PhotonNetwork.AuthValues.AuthType = CustomAuthenticationType.Epic;
    421.  
    422.                 PhotonNetwork.AuthValues.UserId = loginCallbackInfo.SelectedAccountId.ToString(); // alternatively set by server
    423.  
    424.                 Debug.Log("EPIC ACCESS TOKEN: "+loginCallbackInfo);
    425.  
    426.                 PhotonNetwork.AuthValues.AddAuthParameter("token", loginOptions.Credentials.Value.Token);
    427.  
    428.                 PhotonNetwork.ConnectUsingSettings();
    429.  
    430.                 OnConnectedToMaster();
    431.  
    432.  
    433.             }
    434.  
    435.             else if (Common.IsOperationComplete(loginCallbackInfo.ResultCode))
    436.  
    437.             {
    438.  
    439.                 Debug.Log("Login failed: " + loginCallbackInfo.ResultCode);
    440.  
    441.             }
    442.  
    443.         });      
    444.  
    445.     }
    446.  
    447.     #endregion
    448.  
    449.  
    450.     #region Photon LogIn Anonymously
    451.  
    452.     private void photonAnonymousLogin()
    453.  
    454.     {
    455.  
    456.         string playerName = playerNameInputField.text;
    457.  
    458.         if(!string.IsNullOrEmpty(playerName))
    459.  
    460.         {
    461.  
    462.          
    463.  
    464.             showConnectionStatus = true;
    465.  
    466.             uI_connectionStatus.SetActive(true);
    467.  
    468.             PhotonNetwork.LocalPlayer.NickName = playerName;
    469.  
    470.             PhotonNetwork.ConnectUsingSettings();
    471.  
    472.         }
    473.  
    474.         else
    475.  
    476.         {
    477.  
    478.             Debug.Log("Player name is invalid or empty! "+ playerName);
    479.  
    480.         }
    481.  
    482.     }
    483.  
    484.  
    485.     #endregion
    486.  
    487.  
    488.     #region "PRIVATE methods"
    489.  
    490.     private void setAuthFieldsForProfileInterface()
    491.  
    492.     {
    493.  
    494.         if(FB.IsLoggedIn)
    495.  
    496.         {
    497.  
    498.             profilePicture_obj.GetComponent<Image>().sprite = profileBackgroundIcon_obj.GetComponent<Image>().sprite;
    499.  
    500.         }
    501.  
    502.     }
    503.  
    504.  
    505.     private void GetFBPicture(IGraphResult result)
    506.  
    507.     {
    508.  
    509.         if (result.Error == null)
    510.  
    511.         {        
    512.  
    513.             Image img = profileBackgroundIcon_obj.GetComponent<Image>();
    514.  
    515.             img.sprite = Sprite.Create(result.Texture, new Rect(0,0, 700, 700), new Vector2());      
    516.  
    517.         }
    518.  
    519.         else
    520.  
    521.         {
    522.  
    523.             Debug.Log("fb picture error: " + result.Error);
    524.  
    525.         }
    526.  
    527.  
    528.     }
    529.  
    530.     private void GetFBFields(IGraphResult result)  
    531.  
    532.     {
    533.  
    534.         if (result.Error == null)
    535.  
    536.         {
    537.  
    538.             Text txtEmail = profileEmail_obj.GetComponent<Text>();        
    539.  
    540.             Text txtName = profileNickname_obj.GetComponent<Text>();
    541.  
    542.             string name;
    543.  
    544.  
    545.             if( result.ResultDictionary.TryGetValue("name", out name))
    546.  
    547.             {
    548.  
    549.                 txtName.text = name;
    550.  
    551.                 Debug.Log("username: "+name);
    552.  
    553.             }                
    554.  
    555.         }
    556.  
    557.         else
    558.  
    559.         {
    560.  
    561.             Debug.LogError(result.Error);
    562.  
    563.         }
    564.  
    565.     }
    566.  
    567.  
    568.     //close epic interface
    569.  
    570.     private void OnApplicationQuit()
    571.  
    572.     {
    573.  
    574.         if (s_PlatformInterface != null)
    575.  
    576.         {
    577.  
    578.             s_PlatformInterface.Release();
    579.  
    580.             s_PlatformInterface = null;
    581.  
    582.             PlatformInterface.Shutdown();
    583.  
    584.         }
    585.  
    586.  
    587.     }
    588.  
    589.     #endregion
    590.  
    591.  
    592.     #region OnConnection done methods
    593.  
    594.     public override void OnConnectedToMaster()
    595.  
    596.     {
    597.  
    598.         Debug.Log("Successfully connected to Photon!");
    599.  
    600.         showConnectionStatus = false;
    601.  
    602.         uI_connectionStatus.SetActive(false);
    603.  
    604.      
    605.  
    606.         uI_Authentication.SetActive(false);
    607.  
    608.         uI_Profile.SetActive(true);
    609.  
    610.     }
    611.  
    612.  
    613.     public override void OnCustomAuthenticationFailed(string debugMessage)
    614.  
    615.     {
    616.  
    617.         Debug.LogErrorFormat("Error authenticating to Photon using "+PhotonNetwork.AuthValues.AuthType+": {0}", debugMessage);
    618.  
    619.     }
    620.  
    621.     #endregion
    622.  
    623. }
    If you need other details, feel free to ask. Thanks in advance!

    Best Regards,

    A
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,066
    There are two separate Facebook authentication APIs. Maybe you mix Facebook Games with Facebook?
    If that's not a hint, we can take a look at your config and try to figure out what may be wrong. Mail us (developer@photonengine.com) and let us know your AppId and which client SDK you use.
     
  3. Akaton97

    Akaton97

    Joined:
    Jun 26, 2017
    Posts:
    6
    Thank you very much!
    I was using base Facebook configuration for facebook unity sdk. Switching to Facebook Gaming, both for meta and photon side, has made everything working again. Thanks again!
     
  4. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,066
    Thanks for the update. Glad this helped!