如何在Facebook上授权应用程序

时间:2016-07-21 05:05:24

标签: c# asp.net facebook post

我正在开发一个asp.net网站,我想从我的网站发布到Facebook,所以我在Facebook创建了一个应用程序,我使用下面的代码,但我运行我的网站我得到错误

代码:

private void Authorization()
    {
        string AppId = "1138658742823914";
        string AppSecret = "87471caa78f52e3919e43c3dc72b542a";
        string scope = "‎public_profile‎,user_friends";
        if (Request["code"]==null)
        {
            Response.Redirect(string.Format("https://graph.facebook.com/oauth/authorize?client_id={0}&redirect uri={1}&scope={2}",AppId,Request.Url.AbsoluteUri,scope));
        }
        else
        {
            Dictionary<string, string> tokens = new Dictionary<string, string>();
            string url = string.Format("https://graph.facebook.com/oauth/access_token?client id={0}&redirect uri={1}&scope={2}&code={3}&client_secret={4}",AppId,Request.Url.AbsoluteUri,scope,Request["code"].ToString(),AppSecret);
            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('&'))
                {
                    // meh.aspx?token1=steve&token2=jack&....
                    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="Atefeh Ahmadi Hello every One"});

        }

错误:

Facebook.FacebookOAuthException: (OAuthException - #200) (#200) The user hasn't authorized the application to perform this action

See Picture

1 个答案:

答案 0 :(得分:-1)

看起来您还没有在Facebook中正确配置您的应用,或者您在Facebook上创建应用时没有正确提及您的网站网址。