Facebook连接无法连接到Unity中的GameSparks服务器

时间:2019-07-12 17:27:09

标签: facebook unity3d gamesparks

我正在使用Facebook SDK for Unity通过FacebookConnectRequest设置与GameSparks服务器的Facebook连接。但是,我收到的错误响应是键“ accessToken”和值“ NOTAUTHENTICATED”。错误的详细信息是“系统无法验证令牌。”。

我试图在Unity中重新导入Facebook和GameSparks SDK。在代码中更改Facebook和GameSpark的一些初始化。但是,我无法提出解决方案。

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
    }

}

我要删除GameSparks请求的FacebookConnectRequest的错误响应。

1 个答案:

答案 0 :(得分:0)

由于derHugo的建议,我已经解决了问题。由于某些原因,acces令牌在FacebookConnectionRequest之前已损坏。为了防止有关访问令牌的任何不良情况,需要手动刷新它。为此,需要在FacebookConnectionRequest之前使用FB.Mobile.RefreshCurrentAccessToken。

对该功能的解释如下:可能需要手动刷新用户授予应用程序的当前访问令牌,以便检索最新的权限,并延长有效期(如果扩展名是)。可能。使用FB.Mobile.RefreshCurrentAccessToken完成此操作。 (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
        {
            ...
        }