如何使用facebook Oauth2身份验证?

时间:2016-06-16 04:13:32

标签: c# facebook-graph-api

我正在尝试使用Facebook API来验证登录。

我在该网址中收到错误。

我不知道创建网址的确切格式。

你能帮我解决吗?

if(Request["code"]==null)
{
    Response.Redirect(string.Format("https://graph.facebook.com/oauth2/access_token?client_id={0}&grant_type={1}&scope{2}",
    app_id, Request.Url.AbsoluteUri, scope));
}
else
{
    Dictionary<string, string> tokens = new Dictionary<string, string>();

    string url = string.Format("https://graph.facebook.com/oauth2/access_token?client_id={0}&grant_type={1}&scope={2}&code={3}&client_secret={4}",
        app_id, Request.Url.AbsoluteUri, scope, Request["code"].ToString(), app_secret);

    HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

    using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
    {
        StreamReader reader = new StreamReader(response.GetResponseStream());

        string vals = reader.ReadToEnd();

        foreach(string token in vals.Split('&'))
        {
            tokens.Add(token.Substring(0, token.IndexOf("=")), token.Substring(token.IndexOf("=") + 1, token.Length - token.IndexOf("=") - 1));
        }
    }

    string access_token = tokens["access_token"];

    var client = new FacebookClient(access_token);

    client.Post("me/feed", new { message = "Posted this using Api" });

}

当我运行此程序时,我在浏览器上收到此错误。

{
    "error": {
        "message": "Unknown path components: /access_token",
        "type": "OAuthException",
        "code": 2500,
        "fbtrace_id": "ENToUCpZz/u"
    }
}

0 个答案:

没有答案