1

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.

public void ConnectPlayerViaFacebook()
{
    ChangeCurrentText("Connecting Facebook With Server...");
    Debug.Log("Connecting Facebook With GameSparks...");// first check if FB is ready, and then login //
                                                        // if it's not ready we just init FB and use the login method as the callback for the init method //
    if (!FB.IsInitialized)
    {
        ChangeCurrentText("Initializing Facebook...");
        Debug.Log("Initializing Facebook...");
        FB.Init(ConnectGameSparksToGameSparks, null);
    }
    else
    {
        FB.ActivateApp();
        ConnectGameSparksToGameSparks();
    }
}

///<summary>
///This method is used as the delegate for FB initialization. It logs in FB
/// </summary>
private void ConnectGameSparksToGameSparks()
{
    if (FB.IsInitialized)
    {
        FB.ActivateApp();
        Debug.Log("Logging into Facebook...");
        ChangeCurrentText("Logging into Facebook...");
        var perms = new List<string>() { "public_profile", "email" };
        FB.LogInWithReadPermissions(perms, (result) =>
        {
            if (FB.IsLoggedIn)
            {
                ChangeCurrentText("Logged in, Connecting Server via Facebook...");
                new FacebookConnectRequest()
                .SetAccessToken(AccessToken.CurrentAccessToken.TokenString)
                .SetDoNotCreateNewPlayer(false)
                .SetDoNotLinkToCurrentPlayer(false)
                .SetSwitchIfPossible(false)
                .SetSyncDisplayName(true)
                .Send((fbauth_response) =>
                {
                    if (!fbauth_response.HasErrors)
                    {
                        ...
                    }
                    else
                    {
                        Debug.Log(fbauth_response.Errors.JSON.ToString());

                        ChangeCurrentText("Server Authentication with Facebook Failed!" + fbauth_response.Errors.JSON.ToString());
                    }
                });
            }
            else
            {
                Debug.LogWarning("Facebook Login Failed!" + result.Error.ToString());


                ChangeCurrentText("Facebook Login Failed!" + result.Error.ToString());
            }
        });
    }
    else
    {
        ConnectPlayerViaFacebook(); // If we are still not connected, then try to process again
    }

}

I want to remove the error response of the FacebookConnectRequest of the GameSparks request.

derHugo
  • 83,094
  • 9
  • 75
  • 115
TumayVarel
  • 11
  • 4
  • Is the token maybe expired? `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](https://developers.facebook.com/docs/unity/reference/current/Properties/?locale=de_DE)) – derHugo Jul 12 '19 at 19:20
  • Thanks, I don't know how I couldn't think this. Before the Facebook connection request, refreshing the access token solved my problem. – TumayVarel Jul 13 '19 at 15:08

1 Answers1

0

Thanks to derHugo's advice I have solved the problem. For some reasons the acces token is broken before the FacebookConnectionRequest. 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)

if (FB.IsLoggedIn)
        {
            FB.Mobile.RefreshCurrentAccessToken(callback =>
            {
                    ...
            });
            ChangeCurrentText("Logged in, Connecting Server via Facebook...");
            new FacebookConnectRequest()
            .SetAccessToken(AccessToken.CurrentAccessToken.TokenString)
            .SetDoNotCreateNewPlayer(false)
            .SetDoNotLinkToCurrentPlayer(false)
            .SetSwitchIfPossible(false)
            .SetSyncDisplayName(true)
            .Send((fbauth_response) =>
            {
                ...
            });
        }
        else
        {
            ...
        }
TumayVarel
  • 11
  • 4