This project has moved. For the latest updates, please go here.
1

Closed

WP7: Cannot log into Facebook: KeyNotFoundException

description

Hi Prabir, I'm opening the issue per your request in this discussion:
http://facebooksdk.codeplex.com/discussions/284103

It seems like every time I try to login I get a KeyNotFoundException. It's rendering the application unusable since there's no graceful way out of this exception, at least none that I know. Thank you for your quick response!

comments from the discussion thread:
johnwarns
Sat at 2:16 PM


any one else getting a KeyNotFoundException error when running the Windows phone sample?

john

Reply
Reply with quote
Bob_12345
Today at 10:14 PM


I just started having this issue recently, out of the blue. The following exception occurs:

System.Collections.Generic.KeyNotFoundException was unhandled
Message=KeyNotFoundException
StackTrace:
   at System.ThrowHelper.ThrowKeyNotFoundException()
   at System.Collections.Generic.Dictionary`2.get_Item(String key)
   at Facebook.JsonObject.get_Item(String key)
   at Facebook.FacebookOAuthClient.BuildExchangeCodeForAccessTokenResult(String json)
   at Facebook.FacebookOAuthClient.<ExchangeCodeForAccessTokenAsync>b__6(String json)
   at Facebook.FacebookOAuthClient.<>c__DisplayClass9.<OAuthRequestAsync>b__8(Object o, OpenReadCompletedEventArgs e)
   at FluentHttp.HttpHelper.OnOpenReadCompleted(OpenReadCompletedEventArgs args)
   at FluentHttp.HttpHelper.ResponseCallback(IAsyncResult asyncResult, Object userToken)
   at FluentHttp.HttpHelper.OpenReadAsync(Object userToken)
   at FluentHttp.HttpHelper.OpenReadAsync()
   at Facebook.FacebookOAuthClient.<>c__DisplayClass9.<OAuthRequestAsync>b__8(Object o, OpenReadCompletedEventArgs e)
   at FluentHttp.HttpHelper.OnOpenReadCompleted(OpenReadCompletedEventArgs args)
   at FluentHttp.HttpHelper.ResponseCallback(IAsyncResult asyncResult, Object userToken)
   at FluentHttp.HttpHelper.<>c__DisplayClass2.<OpenReadAsync>b__0(IAsyncResult ar)
   at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClassa.<InvokeGetResponseCallback>b__8(Object state2)
   at System.Threading.ThreadPool.WorkItem.WaitCallback_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadPool.WorkItem.doWork(Object o)
   at System.Threading.Timer.ring()
Edit
Reply
Reply with quote
Bob_12345
Today at 10:24 PM


I think the indexer in that Json object implementation is failing. It's really rendering the whole application useless. I think the problem is here, but I'm not 100% certain:

private object BuildExchangeCodeForAccessTokenResult(string json)
{
var returnParameter = new JsonObject();
FacebookUtils.ParseQueryParametersToDictionary("?" + json, returnParameter);

// access_token=string&expires=long or access_token=string
// Convert to JsonObject to support dynamic and be consistent with the rest of the library.
var jsonObject = new JsonObject();
jsonObject["access_token"] = returnParameter["access_token"]; <<<<<<<<Here
....

//Since his/her indexer implementation for JsonObject in JsonSimple.CS, no check for "ContainsKey":
public object this[string key]
{
get { return _members[key]; }
set { _members[key] = value; }
}


Edit
Reply
Reply with quote
prabirshrestha
Coordinator
Today at 10:28 PM


seems like it needs the following line of code

var jsonObject = new JsonObject();

if(jsonObject.ContainsKey("access_token"))
jsonObject["access_token"] = returnParameter["access_token"];


could you either file a new issue or send a pull request.

thanks,

prabir

comments

Bob_12345 wrote Dec 26, 2011 at 7:52 AM

Prabir, I think you meant to do this, check returnParameter:
        var jsonObject = new JsonObject();

        if (returnParameter.ContainsKey("access_token"))
        {
            jsonObject["access_token"] = returnParameter["access_token"];
        }
With this change the http login works but when I set the facebook user to use secure connection (Account Settings -> Security Secure Browsing
Browse Facebook on a secure connection (https) when possible) I get (OAuthException Error validating verification code). Any suggestions?
Thanks!

prabirshrestha wrote Dec 26, 2011 at 8:25 AM

what is your redirect_uri?

if you don't set it manually it sets to "http://www.facebook.com/connect/login_success.html"

you will need to set the https manually.
oauthClient.RedirectUri = new Uri("https://www.facebook.com/connect/login_success.html")

Bob_12345 wrote Dec 26, 2011 at 11:26 AM

Thanks again, prabir. I just used the same callback url i have for Twitter and it worked. My problem was that I was not adding the URL to my app settings in "Select how your app integrates with Facebook"

ldcontact wrote Feb 13, 2012 at 8:38 PM

I am having the same problem. What a bummer!!!

ldcontact wrote Feb 13, 2012 at 9:06 PM

fyi, I just confirmed that if you disable the "secure browsing" in the FB settings, it works fine

ldcontact wrote Feb 13, 2012 at 9:42 PM

I guess the problem will still exists for the users that have selected "secure browsing" option. Is there a workaround for those users? Any recommendations?

prabirshrestha wrote Feb 13, 2012 at 10:53 PM

please file the issue in github instead if u are still having problems. https://github.com/facebook-csharp-sdk/facebook-csharp-sdk/issues/new