如何在facebook中授权应用程序

本文关键字:授权 应用程序 facebook | 更新日期: 2023-09-27 17:57:53

我正在开发一个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

参见图片

如何在facebook中授权应用程序

看起来你没有在facebook中正确配置你的应用程序,或者在facebook上创建应用程序时没有正确提及你的网站url。