Search Unity

Facebook Connection Failed to GameSparks Server in Unity

Discussion in 'Multiplayer' started by tumayvarel, Jul 12, 2019.

  1. tumayvarel

    tumayvarel

    Joined:
    Mar 10, 2019
    Posts:
    6
    First of all, I am not sure this is the right place for this question. So if it is not please, forgive me.

    I'm setting up the Facebook Connection to GameSparks server with FacebookConnectRequest using Facebook SDK for Unity. However, I'm getting an error response with key "accessToken" and value "NOTAUTHENTICATED". The details of the error is "The system was unable to authenticate the token.".

    I have tried to reimport Facebook and GameSparks SDK in Unity. Change the some initialization of the Facebook and GameSpark in the code. However, I could not come up with a solution.

    Code (CSharp):
    1. public void ConnectPlayerViaFacebook()
    2. {
    3.     ChangeCurrentText("Connecting Facebook With Server...");
    4.     Debug.Log("Connecting Facebook With GameSparks...");// first check if FB is ready, and then login //
    5.                                                         // if it's not ready we just init FB and use the login method as the callback for the init method //
    6.     if (!FB.IsInitialized)
    7.     {
    8.         ChangeCurrentText("Initializing Facebook...");
    9.         Debug.Log("Initializing Facebook...");
    10.         FB.Init(ConnectGameSparksToGameSparks, null);
    11.     }
    12.     else
    13.     {
    14.         FB.ActivateApp();
    15.         ConnectGameSparksToGameSparks();
    16.     }
    17. }
    18.  
    19. ///<summary>
    20. ///This method is used as the delegate for FB initialization. It logs in FB
    21. /// </summary>
    22. private void ConnectGameSparksToGameSparks()
    23. {
    24.     if (FB.IsInitialized)
    25.     {
    26.         FB.ActivateApp();
    27.         Debug.Log("Logging into Facebook...");
    28.         ChangeCurrentText("Logging into Facebook...");
    29.         var perms = new List<string>() { "public_profile", "email" };
    30.         FB.LogInWithReadPermissions(perms, (result) =>
    31.         {
    32.             if (FB.IsLoggedIn)
    33.             {
    34.                 ChangeCurrentText("Logged in, Connecting Server via Facebook...");
    35.                 new FacebookConnectRequest()
    36.                 .SetAccessToken(AccessToken.CurrentAccessToken.TokenString)
    37.                 .SetDoNotCreateNewPlayer(false)
    38.                 .SetDoNotLinkToCurrentPlayer(false)
    39.                 .SetSwitchIfPossible(false)
    40.                 .SetSyncDisplayName(true)
    41.                 .Send((fbauth_response) =>
    42.                 {
    43.                     if (!fbauth_response.HasErrors)
    44.                     {
    45.                         ...
    46.                     }
    47.                     else
    48.                     {
    49.                         Debug.Log(fbauth_response.Errors.JSON.ToString());
    50.  
    51.                         ChangeCurrentText("Server Authentication with Facebook Failed!" + fbauth_response.Errors.JSON.ToString());
    52.                     }
    53.                 });
    54.             }
    55.             else
    56.             {
    57.                 Debug.LogWarning("Facebook Login Failed!" + result.Error.ToString());
    58.  
    59.  
    60.                 ChangeCurrentText("Facebook Login Failed!" + result.Error.ToString());
    61.             }
    62.         });
    63.     }
    64.     else
    65.     {
    66.         ConnectPlayerViaFacebook(); // If we are still not connected, then try to process again
    67.     }
    68.  
    69. }
    I want to remove the error response of the FacebookConnectRequest of the GameSparks request to connect succesfully to gameSparks with Facebook.
     
  2. tumayvarel

    tumayvarel

    Joined:
    Mar 10, 2019
    Posts:
    6
    I have solved the problem. To prevent any undesirable situation about access token it needs to be manually refreshed. To do it, FB.Mobile.RefreshCurrentAccessToken needs to be used before FacebookConnectionRequest.

    The explanation of that function is as follows: It may be desireable to manually refresh the current access token granted to the application by the user in order to retrieve up-to-date permissions, and extend the expiration date, if extension is possible. Use FB.Mobile.RefreshCurrentAccessToken to accomplish this. (source)

    Code (CSharp):
    1. if (FB.IsLoggedIn)
    2.         {
    3.             FB.Mobile.RefreshCurrentAccessToken(callback =>
    4.             {
    5.                     ...
    6.             });
    7.             ChangeCurrentText("Logged in, Connecting Server via Facebook...");
    8.             new FacebookConnectRequest()
    9.             .SetAccessToken(AccessToken.CurrentAccessToken.TokenString)
    10.             .SetDoNotCreateNewPlayer(false)
    11.             .SetDoNotLinkToCurrentPlayer(false)
    12.             .SetSwitchIfPossible(false)
    13.             .SetSyncDisplayName(true)
    14.             .Send((fbauth_response) =>
    15.             {
    16.                 ...
    17.             });
    18.         }
    19.         else
    20.         {
    21.             ...
    22.         }