[英]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. 现在,我在过去的5个小时中一直停留在facebook身份验证上,我对为什么会出现错误的想法没有太多思考:400错误的请求。 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. 使用示例应用程序,我没有任何问题,但是在应用程序中,我试图使用代码,但出现400错误的请求错误。 The url parameter looks pretty much the same and that's why I don't have idea what I am doing wrong.
url参数看起来几乎相同,这就是为什么我不知道自己在做什么错的原因。 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
非常感谢Laziale
Facebook tends to not like localhost urls. Facebook倾向于不喜欢本地主机URL。 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.
您可以在主机文件(例如fbdev.com)中为伪造的域创建一个条目,并在开发人员控制台的应用程序基本设置中将该域(带有端口)设置为应用程序URL和域。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.