- Home /
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 ?
Answer by Tekksin · Dec 26, 2014 at 04:45 PM
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:\n" + 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:\n" + 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.
for some this might work....
just replace IDictionary dict = Facebook.$$anonymous$$iniJSON.Json.Deserialize(result.Text) as IDictionary; to IDictionary dict = Facebook.$$anonymous$$iniJSON.Json.Deserialize(result.RawResult) as IDictionary;
this worked for me
Answer by AshyB · Jul 07, 2017 at 02:19 AM
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.
This almost works for me. It gives the user's name as expected, but their whole name. I only want their first name.
Answer by Swaminathan · Feb 18, 2014 at 12:12 PM
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.
do you put this above the variables? FB.API("me?fields=name", Facebook.Http$$anonymous$$ethod.GET, UserCallBack);
i got error.
Answer by Caffeinebomb · Jan 30, 2014 at 05:14 PM
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.
I know that I need to put a callback, but I dont understood what I need to put on the function callback