发布到Facebook页面作为页面

时间:2015-05-19 18:23:18

标签: c# facebook facebook-graph-api facebook-c#-sdk

经过深入搜索,我能够像Facebook一样在Facebook页面上发帖。但我的目标是在页面上发布我作为页面管理。我做了一些研究,我能够获得我管理的页面的访问令牌。当我试图POST它时,sdk给了我错误

The user hasn't authorized the application to perform this action

这是我的代码供您参考:

        string app_id = "xxxxx";
        string app_secret = "yyyyyyy";
        string scope = "publish_actions,manage_pages";

        if (Request["code"] == null)
        {
            string url = string.Format(
                "https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}&scope={2}",
                app_id, Request.Url.AbsoluteUri, scope);
            Response.Redirect(url, false);
        }
        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}",
                app_id, Request.Url.AbsoluteUri, scope, Request["code"].ToString(), app_secret);

            HttpWebRequest request = System.Net.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);

            dynamic parameters = new ExpandoObject();
            parameters.message = "Check out this funny article";
            parameters.link = "http://www.natiska.com/article.html";
            parameters.picture = "http://www.natiska.com/dav.png";
            parameters.name = "Article Title";
            parameters.caption = "Caption for the link";

            //zzzzzzz is my fan page
            string pageAccessToken = ""; 
            JsonObject jsonResponse = client.Get("me/accounts") as JsonObject;
            foreach (var account in (JsonArray)jsonResponse["data"])
            {
                string accountName = (string)(((JsonObject)account)["name"]);

                if (accountName == MY_PAGE_NAME)
                {
                    pageAccessToken = (string)(((JsonObject)account)["access_token"]);
                    break;
                }
            }

            client = new FacebookClient(pageAccessToken);
            client.Post("/zzzzzzz/feed", parameters);

2 个答案:

答案 0 :(得分:4)

您需要使用publish_pages权限发布为网页。

  

https://developers.facebook.com/docs/graph-api/reference/v2.3/page/feed#publish

答案 1 :(得分:0)

Your Facebook App Dosn't have manage_page & publish_page permission, instead of this you can create test application and from that u can create test users for your testing . From there u can manage all the pages posts and data.