1
votes

When I try to get the users data with the:

dynamic myInfo = fb.Get("me")

method I've encoutered this message:

The given key was not present in the dictionary.

This error is only happeing with few users, but still, I need to fix it.

Those are the values that I'm trying to read from facebook:

myInfo["id"], myInfo["first_name"], myInfo["last_name"], myInfo["email"], myInfo["name"], myInfo["gender"], myInfo["locale"]

Any idea what could cuase this error ?

2
Venturing a guess here: the key isn't in the dictionary? - user47589

2 Answers

2
votes

I guess that some of the data fields you are requesting are not present for certain users, most likely their email address as this requires "email" extended permission to your application (all the other data fields are publicly available according to the Facebook documentation).

It would be best to check that the key exists in myInfo before trying to access it like this:

if (myInfo.ContainsKey("email") == true)
{
    do something with email
}

You could also use this check to log which keys are not available for which users which will help you investigate any other potential issues.

0
votes

I used Andy's suggestion to implement a code pattern with this structure:

// create a "me" object
JSONObject me = fb.Get("me");

// get first name
string firstName = string.Empty;
if (me.Dictionary.ContainsKey("first_name"))
{
    firstName = me.Dictionary["first_name"].String;
}