Retrieve player info Facebook API

After following the facebook tutorial (https://developers.facebook.com/docs/games/unity/unity-tutorial) I’ve tried to adapt that information to my game.

I’ve already managed to make the login, the share function and retrieve some information like the user picture. But I’m not being able to make this work:

private static Dictionary<string, string>   profile         = null;

void OnLoggedIn()                                                                          
	{                                                                                          
		Debug.Log("Logged in. ID: " + FB.UserId);                                                                               
		FB.API("/me?fields=name,email", Facebook.HttpMethod.GET, APICallback);
		PlayerPrefs.SetString("username",profile["first_name"]);
		PlayerPrefs.SetString("id",profile["id"]);

	}   

void APICallback(FBResult result)                                                                                              
	{                                                                                                                              
		Util.Log("APICallback");                                                                                                
		if (result.Error != null)                                                                                                  
		{                                                                                                                          
			Util.LogError(result.Error);                                                                                           
			// Let's just try again                                                                                                
			FB.API("/me?fields=id,first_name", Facebook.HttpMethod.GET, APICallback);     
			return;                                                                                                                
		}                                                                                                                          
		
		profile = Util.DeserializeJSONProfile(result.Text); 


	}   

I made that to try to retrieve the user name and player id. But doesn’t work :
Can someone tell me why?

What you’re doing here:

FB.API("/me?fields=name,email", Facebook.HttpMethod.GET, APICallback);

is calling Facebook and setting the method APICallback as the return method. So what you’ll need to do is create APICallback

void APICallback (FBresult response) {
     Debug.Log(response.text);
}

This should show you the right information. Of course, since it’s one big string, you want to ‘extract’ the data as variables. The data is stored in a JSON-hierarchy (similar to XML). What you need is a JSON-convertor to convert it to a class. You can use JsonFx for this. GitHub - jsonfx/jsonfx: JsonFx v2.0 - JSON serialization framework for .NET .

You’ll be able to use jsonFX by creating a new class called FBData (actually you can name it whatever you want):

public struct FBData {
    string first_name;
    string id;

}

and adding this:

 void APICallback (FBresult response) {
         Debug.Log(response.text);
         FBData data = JsonFx.Json.JsonRead.Deserialize<FBData>(response.Text);
 }

Now you can just send data anywhere and access variables by using data.first_name, data.id and anything you call and want to add.

Sounds difficult? It’s not, just go step by step.

[Edit] Capital T in in ‘response.Text’

I found that if you make a new function in Util to be value dependant and use that, you can get other values - I have used it to get last_name, gender. It doesn’t seem to work for email or age_range but that maybe something I have done (permissions are not straight forward).

Here is the code for the new function, I have put this in Util.cs:

public static string GetStringDeserializeJSON(string response, string field)
{
	var responseObject = Json.Deserialize(response) as Dictionary<string, object>;
	object nameH;
	var profile = new Dictionary<string, string>();
		
	if (responseObject.TryGetValue(field, out nameH)){
		profile[field] = (string)nameH;
	}

	Debug.Log ("Data: " + field + " " + profile [field]);
	
	return (string)profile[field];
}