简体   繁体   中英

Facebook integration for login

I am having trouble with integration with Facebook for my site.

Here is the code:

private static string GetFacebookUserJSON(string access_token)
{
    string url = string.Format(
        "https://graph.facebook.com/me?access_token={0}&fields=email,name,first_name,last_name,link", 
        access_token);

    WebClient wc = new WebClient();
    Stream data = wc.OpenRead(url);
    StreamReader reader = new StreamReader(data);
    string s = reader.ReadToEnd();
    data.Close();
    reader.Close();

    return s;
}

Unfortunately, it gives me a WebException was unhandled by user code on the OpenRead() line. Additionally, I have the information that I got a (400) Bad Request from the contacted server.

How can I solve this?

Use this to detect the exact error:

private static string GetFacebookUserJSON(string access_token)
{
    try
    {
      string url = string.Format("https://graph.facebook.com/me?access_token={0}&fields=email,name,first_name,last_name,link", access_token);

      WebClient wc = new WebClient();
      Stream data = wc.OpenRead(url);
      StreamReader reader = new StreamReader(data);
      string s = reader.ReadToEnd();
      data.Close();
      reader.Close();

      return s;
    }

    catch (WebException wex)
    {
        string pageContent = new StreamReader(wex.Response.GetResponseStream()).ReadToEnd().ToString();
        return pageContent;
    }
}

Or I would suggest using Fiddler and updating your question with the response from it's log

Have a further look here for more information about Facebook login securities.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM