简体   繁体   中英

Cannot post user score in Facebook SDK for Unity

I'm working in a Unity3D game for android that uses the Facebook Unity SDK to store, retrieve the scores and make leaderboards, but I've found problems and I cannot figure out the reason.

I'm using:

  • Unity Pro 5.3.3f1.
  • Facebook Unity SDK 7.4.0.
  • Graph API 2.5.

I posted the code which I think is making trouble. It sometimes works, but it mainly fails by not having required permissions or by stating a "not found" error.

The game is correctly configured in the Facebook developers web page.

--

public void RequestSetScore(FacebookScore facebookScore)
{
    if (FacebookManager.Instance.accessToken == null)
    {
        FacebookManager.Instance.OnRequestSetScoreError("Application has not logged in to Facebook yet.");
        return;
    }

    if (this.PermissionIsGranted("publish_actions"))
    {
        string requestUri;
        requestUri = string.Format("{0}/scores?score={1}",
            FacebookManager.Instance.accessToken.UserId,
            facebookScore.ScoreValue.ToString());

        FB.API(requestUri, HttpMethod.POST,
            scoreSetRequestresult =>
            {
                // Esto se ejecuta cuando la respuesta se ha recibido, no inmediatamente
                if (scoreSetRequestresult.Error != null)
                {
                    // This is an event of my class
                    FacebookManager.Instance.OnRequestSetScoreError(scoreSetRequestresult.Error);
                }
                else
                {
                    // This is an event of my class
                    FacebookManager.Instance.OnRequestSetScoreSuccess();
                }
            }
        );
    }
    else
    {
        // Pedir permisos para publicar puntuaciones
        FB.LogInWithPublishPermissions(FacebookManager.Instance.publishPermissions,
            permissionRequestresult =>
            {
                // Esto se ejecuta cuando la respuesta se ha recibido, no inmediatamente
                if (permissionRequestresult.Error != null)
                {
                    string requestUri;
                    requestUri = string.Format("{0}/scores?score={1}",
                        FacebookManager.Instance.accessToken.UserId,
                        facebookScore.ScoreValue.ToString());

                    FB.API(requestUri, HttpMethod.POST,
                        scoreSetRequestresult =>
                        {
                            // Esto se ejecuta cuando la respuesta se ha recibido, no inmediatamente
                            if (scoreSetRequestresult.Error != null)
                            {
                                // This is an event of my class
                                FacebookManager.Instance.OnRequestSetScoreError(scoreSetRequestresult.Error);
                            }
                            else
                            {
                                // This is an event of my class
                                FacebookManager.Instance.OnRequestSetScoreSuccess();
                            }
                        }
                    );
                }
                else
                {
                    // This is an event of my class
                    FacebookManager.Instance.OnRequestSetScoreError(permissionRequestresult.Error);
                }
            }
        );
    }
}

// Métodos auxiliares
private bool PermissionIsGranted(string permission)
{
    AccessToken token = Facebook.Unity.AccessToken.CurrentAccessToken;
    foreach (string grantedPermission in token.Permissions)
    {
        if (grantedPermission == permission)
            return true;
    }

    return false;
}

--

Comments about the code: The FacebookScore type is a struct that encapsulates a long, and allows me manipulate it bitwise to store more than one number. These methods belong to a singleton-like class that that is accesible at every point of the game, and is preserved through scene changes.

Any idea would be very valuable to me, thanks in advance.

The problem is solved. I was sending a 64 bit integer, but the Facebook Graph API can only accept a 32 bit unsigned integer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM