Getting user name Facebook

Hey guys, i’m trying to do a Facebook game, and I’m with a problem, I want to get the user name.

This is my try:

public string name;

void Start () 
{
  FB.API("me?fields=name", Facebook.HttpMethod.GET, name);
}

The error:
error CS1501: No overload for method API' takes 3’ arguments

How can I put this info (name) into a string to show the name ?

I would like to update this answer. “using Facebook.minijson;” either doesn’t exist, or it has been updated to: “using Facebook.MiniJSON;” Case sensitive.

Anyways, to get the first name, this is what I did:

Logged in the player, and in the login call back I wrote:

using Facebook.MiniJSON;
public string get_data;
public string fbname;

  void LoginCallback(FBResult result)
    {
        if (result.Error != null)
            lastResponse = "Error Response:

" + result.Error;
else if (!FB.IsLoggedIn)
lastResponse = “Login cancelled by Player”;
else{
lastResponse = “Login was successful!”;
FB.API(“/me?fields=first_name”, Facebook.HttpMethod.GET, LoginCallback2);
}
}

/*
the FB.API has a method that calls a new function.
In this case LoginCallback2, which has the entered
field’s infromation (first_name)
*/

	void LoginCallback2(FBResult result)
	{
		if (result.Error != null)
			lastResponse = "Error Response:

" + result.Error;
else if (!FB.IsLoggedIn)
lastResponse = “Login cancelled by Player”;
else{
IDictionary dict = Facebook.MiniJSON.Json.Deserialize(result.Text) as IDictionary;
fbname = dict[“first_name”].ToString();
print("your name is: " + fbname);
}
}

This is the proper C# formatting. The above accepted answer seems to be written in javascript and C# (a strange amalgamation), and some (all?) users may find things not working.

btw, replace “first_name” with “name” if you want the full name.

I just started doing this and it seems to be simpler now. Make sure your using the facebook api

using Facebook.Unity;

Then query facebook after you’ve logged in;

FB.API("me?fields=name", Facebook.Unity.HttpMethod.GET, GetFacebookData);

And then the call back is;

   void GetFacebookData(Facebook.Unity.IGraphResult result)
   {
        string fbName = result.ResultDictionary["name"].ToString();

        Debug.Log("fbName: " + fbName);
    }

You no longer need to deserialize as facebook provides the dictionary to you directly.

using Facebook.minijson;

FB.API(“me?fields=name”, Facebook.HttpMethod.GET, UserCallBack);

public string get_data;
public string fbname;

void UserCallBack(FBResult result)
{

      if (result.Error != null)
	{                                                                      
		get_data = result.Text;
	}
	else
	{
		get_data = result.Text;
	}
	
	var dict = Json.Deserialize(get_data) as IDictionary;
	fbname =dict ["name"].ToString();
}

}
NOTE : display fbname using GUIText.

add this code to your script where u need username to be fetched, it will definitely help you …
comment your result so i can understand your problem clearly after this… thank you.

I suggest you look at the Facebook developers documentation Unity Tutorial - Personalization as that has an example of how to retrieve the first name for the logged in player.

Looking at your sample code, the FB.API call is using “name” as the third argument when the third argument should be a delegate that will be called when the API returns a result. So that’s not correct.

Hope that helps, I’ve only just looking at Facebook integration myself.