Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi
I designed a website for facebook data access. I am getting an error when the page loads while i was accessing it at localhost:4999 port.
C#
public class FacebookLoginHelper
   {
       public Dictionary<string,> GetAccessToken(string code, string scope,string  redirectUrl)
       {
           Dictionary<string,> tokens = new Dictionary<string,>();
           string clientId = FacebookApplication.Current.AppId;
               //FacebookContext.Current.AppId;
           string clientSecret = FacebookApplication.Current.AppSecret;
           string url = string.Format("https://graph.facebook.com/oauth/access_token?client_id={0}&redirect_uri={1}&client_secret={2}&code={3}&scope={4}",
                           clientId, redirectUrl, clientSecret, code, scope);
           HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
           using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
           {
               StreamReader reader = new StreamReader(response.GetResponseStream());
               string retVal = reader.ReadToEnd();

               foreach (string token in retVal.Split('&'))
               {
                   tokens.Add(token.Substring(0, token.IndexOf("=")),
                       token.Substring(token.IndexOf("=") + 1, token.Length - token.IndexOf("=") - 1));
               }
           }
           return tokens;
       }
   }


Now it shows error 400 at line :
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)

What is the reason ? Please help me out ?

Thanks
Posted
Updated 2-Sep-11 5:53am
v2

1 solution

Place the line which you are having an error in a try-catch block and see what is the exception you are getting while debugging it.

C#
WebRequest request = WebRequest.Create("url");         
try         
{             
  using (WebResponse response = request.GetResponse())
  {
    Console.WriteLine("Won't get here");
  }
}
catch (WebException e)
{
  using (WebResponse response = e.Response)
  {
    HttpWebResponse httpResponse = (HttpWebResponse) response;
    Console.WriteLine("Error code: {0}", httpResponse.StatusCode);
    using (Stream data = response.GetResponseStream())
    {
       string text = new StreamReader(data).ReadToEnd();
       Console.WriteLine(text);
    }
  }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900