(400)在本地主机上运行asp.net站点时出现错误请求错误

时间:2011-09-02 13:59:17

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

我设计了一个用于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)

是什么原因?请帮帮我?

由于

1 个答案:

答案 0 :(得分:0)

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

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

从那时起,你可以检查出错了并修复它。