- Home /
Error using Facebook API to get user's birthday (given key not present in dictionary)
Hi, I have used the Facebook API to get the user's first name and gender fine, but when I try to also get their birthday. I get an error. Here is the code (It works fine for name and gender but not birthday):
void AuthCallBack (IResult result)
{
if (result.Error != null)
{
Debug.Log(result.Error);
}
else
{
if (FB.IsLoggedIn)
{
//Get Username, first parametre is what you want to get, method, callback
FB.API("/me?fields=first_name", HttpMethod.GET, DisplayUsername);
FB.API("/me?fields=gender", HttpMethod.GET, DisplayGender);
FB.API("/me?fields=birthday", HttpMethod.GET, DisplayBirthday);
SceneManager.LoadScene("loggedinpage");
}
else
{
Debug.Log("not logged in");
}
}
}
void DisplayUsername (IResult result)
{
FacebookFirstName = result.ResultDictionary ["first_name"];
Debug.Log(FacebookFirstName.ToString());
}
void DisplayGender(IResult result)
{
FacebookGender = result.ResultDictionary["gender"];
Debug.Log(FacebookGender.ToString());
}
void DisplayBirthday(IResult result)
{
FacebookBirthday = result.ResultDictionary["birthday"];
Debug.Log(FacebookBirthday.ToString());
}
}
And here is the error:
KeyNotFoundException: The given key was not present in the dictionary. System.Collections.Generic.Dictionary`2[System.String,System.Object].get_Item (System.String key) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:150) facebook.DisplayBirthday (IResult result) (at Assets/Scripts/facebook.cs:104) Facebook.Unity.AsyncRequestString+c__Iterator1.MoveNext () (at Assets/FacebookSDK/SDK/Scripts/Utils/AsyncRequestString.cs:133)
Thanks a lot
Answer by Bastienmp · Jun 16, 2016 at 10:10 AM
Hello, Did you request for the birthday permission ? because the birthday is not include in the public_profile default permission, you have to request for user_birthday :)
$$anonymous$$eyNotFoundException: The given key was not present in the dictionary.
System.Collections.Generic.Dictionary`2[System.String,System.Object].get_Item (System.String key) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:150)
Chat$$anonymous$$anager.GetInfoForChat (IResult result) (at Assets/GameWithFriends/Scripts/$$anonymous$$anagers/Chat$$anonymous$$anager.cs:113)
FacebookExample$$anonymous$$anager.<AuthCallback>m__0 (IGraphResult LoginResult) (at Assets/GameWithFriends/Scripts/$$anonymous$$anagers/FacebookExample$$anonymous$$anager.cs:86)
Facebook.Unity.AsyncRequestString+<Start>d__9.$$anonymous$$oveNext ()
UnityEngine.SetupCoroutine.Invoke$$anonymous$$oveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
private void GetInfoForChat(IResult result)
{
UserID = result.ResultDictionary["id"].ToString();
var dictionary = (Dictionary<string, object>)Facebook.$$anonymous$$iniJSON.Json.Deserialize(result.RawResult);
var friendsDict = (Dictionary<string, object>)dictionary["friends"];
var friendsList = (List<object>)friendsDict["data"];
UserFriendsID = new List<string>();
UserFriendsName = new Dictionary<string, string>();
foreach (var dict in friendsList)
{
UserFriendsID.Add(((Dictionary<string, object>)dict)["id"].ToString());
UserFriendsName.Add(((Dictionary<string, object>)dict)["id"].ToString(), ((Dictionary<string, object>)dict)["name"].ToString());
}
///Start chat
Connect();
}
I got this error. Am using PUN with facebook to getting friends list