• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Fred_Vicentin · Jan 30, 2014 at 01:25 PM · facebook

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 ?

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

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.

Comment
Hardik
gulfam131
JohnTube
rajavamsidhar_gvs
Ian094
$$anonymous$$
Meltdown

People who like this

7 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gulfam131 · Jun 28, 2015 at 01:28 PM 1
Share

thanks, you saved my time.

avatar image anmol07goyal · Dec 24, 2020 at 12:49 PM 0
Share

for some this might work....

just replace IDictionary dict = Facebook.MiniJSON.Json.Deserialize(result.Text) as IDictionary; to IDictionary dict = Facebook.MiniJSON.Json.Deserialize(result.RawResult) as IDictionary;

this worked for me

avatar image

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.

Comment

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fred_Vicentin · Jan 30, 2014 at 08:45 PM 0
Share

I know that I need to put a callback, but I dont understood what I need to put on the function callback

avatar image

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.

Comment
Fred_Vicentin

People who like this

1 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vfxjex · Jul 14, 2014 at 09:13 AM 0
Share

do you put this above the variables? FB.API("me?fields=name", Facebook.HttpMethod.GET, UserCallBack);

i got error.

avatar image

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.

Comment
ShantuApps
faithmorante

People who like this

2 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image archelyte_vz · Jul 26, 2017 at 07:28 PM 0
Share

This almost works for me. It gives the user's name as expected, but their whole name. I only want their first name.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Facebook SDK restarts app 1 Answer

Posting to Facebook wall using HttpMethod.POST unity3D 0 Answers

Rewarding Facebook requests 0 Answers

Facebook Post to wall FB.Feed issue in Unity Facebook SDK 1 Answer

Permissions not being granted with Facebook App installed 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges