繁体   English   中英

Facebook登录asp.net 400错误的请求

[英]facebook login in asp.net 400 bad request

现在,我在过去的5个小时中一直停留在facebook身份验证上,我对为什么会出现错误的想法没有太多思考:400错误的请求。 我在网络上找到了一个示例应用程序,用户在此获得了授权,然后继续使用该应用程序。我正在本地运行相同的应用程序,并且能够运行它并正确获取访问令牌。 这是获取令牌的方法:

 /// <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;
        }

当响应尝试从请求对象获取响应时,就会发生问题:

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

使用示例应用程序,我没有任何问题,但是在应用程序中,我试图使用代码,但出现400错误的请求错误。 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

我的应用网址格式:

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

任何建议都将受到欢迎,因为此刻的问题确实在我眼中浮现。 非常感谢Laziale

Facebook倾向于不喜欢本地主机URL。 您可以在主机文件(例如fbdev.com)中为伪造的域创建一个条目,并在开发人员控制台的应用程序基本设置中将该域(带有端口)设置为应用程序URL和域。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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