Click here to Skip to main content
15,891,762 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
While I am trying to integrate Facebook login to my application.I am facing a bad request error. I used this link Get user Facebook details in ASP.NET and C#[^] as reference

What I have tried:

string respnse = "";
       if (Request.QueryString["code"] != null)
       {

           string code = Request.QueryString["code"].ToString();

           oAuthFacebooks fbAC = new oAuthFacebooks(); //Standard FB class file available on net in c#

           try
           {
               fbAC.AccessTokenGet(code);
               respnse = fbAC.Token;
           }
           catch (Exception ex)
           {
               Response.Redirect("http://localhost:8020/App/Login.aspx?error=" + ex.Message);
           }





       }
       else
       {
           Response.Redirect("http://localhost:8020/aspx/SiteLogin.aspxerror=code not found" +
                             Request.QueryString["error_reason"].ToString());
       }

       string token = respnse;
       string HitURL = string.Format("https://graph.facebook.com/me?access_token={0}", token);
       oAuthFacebooks objFbCall = new oAuthFacebooks();
       string JSONInfo = objFbCall.WebRequest(oAuthFacebooks.Method.GET, HitURL, "");
Posted
Updated 23-Nov-20 5:49am

1 solution

Believe that reference is outdated.

Refer this: .net: ASP.Net Web forms: Implementing google and facebook login[^]

Make GET request for token and then use it to go to Facebook.
C#
if (Request["code"] != null)
{
    string url = string.Format(ConfigurationManager.AppSettings["FacebookOAuthurl"],
        ConfigurationManager.AppSettings["Facebook_AppId"],
        ConfigurationManager.AppSettings["Facebook_RedirectUrl"],
        ConfigurationManager.AppSettings["Facebook_scope"],
        Request["code"].ToString(),
        ConfigurationManager.AppSettings["Facebook_AppSecret"]);

    string tokenResponse = MakeWebRequest(url, "GET");
    var tokenInfo = new JavaScriptSerializer().Deserialize<FacebookToken>(tokenResponse);
    var facebookInfoJson = MakeWebRequest(ConfigurationManager.AppSettings["FacebookAccessUrl"] + tokenInfo.access_token,

"GET");
    FacebookInfo objUser = new JavaScriptSerializer().Deserialize<FacebookInfo>(facebookInfoJson);
    txtResponse.Text = facebookInfoJson;
}


public string MakeWebRequest(string destinationUrl, string methodName, string contentType = "", string requestJSON = "")
{
    try
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(destinationUrl);
        request.Method = methodName;
        if (methodName == "POST")
        {
            byte[] bytes = System.Text.Encoding.ASCII.GetBytes(requestJSON);
            request.ContentType = contentType;
            request.ContentLength = bytes.Length;
            using (Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(bytes, 0, bytes.Length);
            }
        }
        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            if (response.StatusCode == HttpStatusCode.OK)
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    return reader.ReadToEnd();
                }
            }
        }

        return null;
    }
    catch (WebException webEx)
    {
        return webEx.Message;
    }
}
 
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