(400) 在本地主机上运行 ASP.NET 站点时发生错误请求错误

发布于 2024-12-02 19:17:43 字数 1471 浏览 0 评论 0原文

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

原因是什么?请帮帮我吗?

谢谢

I am designed a website for facebook data access and while i was accessing it at localhost:4999 port but when ever the page loads then error comes

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

Now it shows error 400 at line :
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)

What is the reason ? Please help me out ?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岛徒 2024-12-09 19:17:43

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

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

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

A 400 Bad Request Error is returned by the Facebook API when one of the parameters isn't valid for the request you're trying to do, you could try manually pasting the requested URL on your browser along with the parameters, it should display the reason of the error as a JSON (On Chrome or Firefox), Something like this for example:

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

From that point on, you can check what's wrong and fix it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文