Search code examples
facebookfacebook-graph-apiunity-game-enginefacebook-unity-sdk

Unity Facebook API - FB.API() Syntax


(Using Unity 5.2.2, Unity Facebook SDK 7.2.2)

I am trying to grab my Facebook profile picture once I am logged in, make it into a Sprite and setting it as an Image. The method FBLogin() is called when a Button is clicked..

public void FBLogin()
{
    perms = new List<string>() { "public_profile", "email", "user_friends" };
    FB.LogInWithReadPermissions(perms, AuthCallback);
}

private void AuthCallback(ILoginResult result)
{
    if (FB.IsLoggedIn)
    {                
        //Get Facebook Details
        getUserInfoFromFacebook();
    }
    else
    {
        Debug.Log("User cancelled login");
    }
}

private void getUserInfoFromFacebook()
{
    FB.API("/v2.5/me?fields=id,name,picture", Facebook.Unity.HttpMethod.GET, DealWithProfilePicture);
}

private void DealWithProfilePicture(IGraphResult result)
{
    if (result.Error != null)
    {
        Debug.Log("Problem with getting profile picture");
        FB.API("/v2.5/me?fields=id,name,picture",Facebook.Unity.HttpMethod.GET, DealWithProfilePicture);
        Debug.Log(result.Error);
        return;

    }

    profile_picture.sprite = Sprite.Create(result.Texture, new Rect(0,0,128,128), new Vector2(0,0));

}

I can't get the hang of the API syntax at all - it produces the error 400 Bad Request. Can somebody help me out with the syntax?

Thanks


Solution

  • You can see in that documentation that the picture is not a field of the user, but an extra edge, which can be retrieved by requesting /me/picture:

    FB.API("/me/picture",HttpMethod.GET,delegate(IGraphResult result) {
        if (string.IsNullOrEmpty(result.Error))
        {
            Debug.Log("received texture with resolution "+result.Texture.width + "x" + result.Texture.height);
            profile_picture.sprite = Sprite.Create(result.Texture, new Rect(0,0,128,128), new Vector2(0,0));
        }
        else
        {
            Debug.LogWarning("received error="+result.Error);
        }
    });