【问题标题】:(400) Bad Request error running asp.net site at local host(400) 在本地主机上运行 asp.net 站点时出现错误请求错误
【发布时间】:2026-01-29 14:15:02
【问题描述】:

我设计了一个用于 facebook 数据访问的网站,当我在 localhost:4999 端口访问它时,当页面加载时就会出现错误

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

现在它在第 400 行显示错误: 使用 (HttpWebResponse response = request.GetResponse() as HttpWebResponse)

是什么原因?请帮帮我?

谢谢

【问题讨论】:

    标签: c# asp.net c#-4.0 httpwebrequest


    【解决方案1】:

    当其中一个参数对您尝试执行的请求无效时,Facebook API 返回 400 Bad Request Error,您可以尝试在浏览器上手动粘贴请求的 URL 以及参数,它应该将错误原因显示为 JSON(在 Chrome 或 Firefox 上),例如:

    {
        "error": {
            "type": "OAuthException",
            "message": "redirect_uri isn't an absolute URI. Check RFC 3986."
    }
    

    从那时起,您可以检查问题所在并进行修复。

    【讨论】:

      最近更新 更多