简体   繁体   中英

facebook login in asp.net 400 bad request

I am stuck with facebook authentication for the last 5 hours now and I don't have a lot of thoughts on my mind why I am getting error: 400 bad request. I found one sample app on the web where user gets authorized and then proceeds with the app, I am running the same app locally and I am able to run it and get the access token correctly. Here is the method to get the token:

 /// <summary>
        /// Get the authorisation token
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public Dictionary<string, string> GetAccessToken(string code, string scope,string  redirectUrl)
        {
            Dictionary<string, string> tokens = new Dictionary<string, string>();
            string clientId = FacebookContext.Current.AppId;            
            string clientSecret = FacebookContext.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;
        }

The problem happens when the response tries to get the response from the request object:

using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)

With the sample app I don't have any problems, but in the app I am trying to use the code I am getting 400 bad request error. The url parameter looks pretty much the same and that's why I don't have idea what I am doing wrong. Here is the sample app format of the url:

https://graph.facebook.com/oauth/access_token?client_id=MY_APP_ID&redirect_uri=http://localhost:5000/fbregcallback.aspx&client_secret=982161272e8adc3af17952dc0f70b67e&code=AQBSvKfdc3ynTWeJTVSq7Y2JRV52YNCHPV5nAluxVf3_Ger68NJZeWScbKg1ttMfVOuXrOTfbvgq6o_bXIemnlx2yH-eu2vDvs-94EDEPxHFPUTQ4UjnSgxKAZjs6h-APxqHrRz5gvlafYb89uIoGhBuu-sIPrvIThBK-BmFRuytrj2fHYSU9y_xbFIjAYNUw_U&scope=user_birthday,publish_stream,create_event,user_photos

My app url format:

https://graph.facebook.com/oauth/access_token?client_id=MY_APP_ID&redirect_uri=http://localhost:47736/Default.aspx&client_secret=982161272e8adc3af17952dc0f70b67e&code=AQBd_IYcBLy-UfV68PDH8gWFOud5eLzVt8ZukSVfo35qSLw7jn5yrpw-zqr4WhVigv0G1tVtEVIj2fnvBAzmWU3SlOtZgCC-0P1sBRQ999JP60Qe4s0UT1c0Z1cupMW8qNdDfuBhzxty9581gTISPq9xYF1SEifX60kDUc1bd7FuUGsGyGBgtfzO8PdNZoGrq6Y&scope=user_birthday,publish_stream,create_event,user_photos

Every advice will be welcomed because this problem really comes on my nerves at this moment. Thanks a lot, Laziale

Facebook tends to not like localhost urls. You can create an entry for a fake domain in your hosts file (eg fbdev.com) and set that domain (with port) as your application URL and domain in the basic settings for your app in the developers console.

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