首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Facebook连接到Unity中的GameSparks服务器失败

Facebook连接到Unity中的GameSparks服务器失败
EN

Stack Overflow用户
提问于 2019-07-13 01:27:09
回答 1查看 179关注 0票数 1

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

我曾尝试在Unity中重新导入Facebook和GameSparks SDK。在代码中更改Facebook和GameSpark的一些初始化。然而,我想不出一个解决方案。

代码语言:javascript
复制
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的错误响应。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-13 23:27:16

多亏了derHugo的建议,我解决了这个问题。由于某些原因,access token在FacebookConnectionRequest之前就被破坏了。为了防止任何关于access token的不良情况,需要手动刷新。为此,需要在FacebookConnectionRequest之前使用FB.Mobile.RefreshCurrentAccessToken。

对该功能的解释如下:可能希望手动刷新用户授予应用程序的当前访问令牌,以便检索最新的许可,并延长到期日期(如果可以延长的话)。使用FB.Mobile.RefreshCurrentAccessToken来实现这一点。(source)

代码语言:javascript
复制
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
        {
            ...
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57011578

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档