如何使用c#sdk将Feed作为管理员发布到Facebook页面?

时间:2013-08-22 06:50:54

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

我想使用c#sdk更新facebook页面。我已经部分成功了,问题是每当我发布消息到页面时,帖子只对管理员可见(我是该页面的管理员)已登录。我希望每个访问该页面的人都能看到帖子或提要。 (即使管理员已退出帖子,管理员也看不到)

以下代码我试图实现

public ActionResult FacebookPagePost()
{
            string app_id = "xxxx";
            string app_secret = "xxx";
            string scope = "publish_stream,manage_pages";
            string page_Id = "xxX";
            if (Request["code"] == null)
            {
                return Redirect(string.Format(
                    "https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}&scope={2}",
                    app_id, Request.Url.AbsoluteUri, scope));
            }
            else
            {
                try
                {

                    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 = 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 fbAccounts = client.Get("/me/accounts");


                    dynamic messagePost = new ExpandoObject();
                    messagePost.picture = "http://pic.com/pic.png";
                    messagePost.link = "http://www.examplearticle.com";
                    messagePost.name = "name goes here";
                    messagePost.description = "description goes here";

                    //Loop over the accounts looking for the ID that matches your destination ID (Fan Page ID)
                    foreach (dynamic account in fbAccounts.data) {
                        if (account.id == page_Id)
                        {
                            //When you find it, grab the associated access token and put it in the Dictionary to pass in the FB Post, then break out.
                            messagePost.access_token = account.access_token;
                            break;
                        }
                    }

                    client.Post("/" + page_Id + "/feed", messagePost);
                }
                catch (FacebookOAuthException ex)
                {

                }
                catch (Exception e)
                {

                }
            }
}

1 个答案:

答案 0 :(得分:0)

1)在developers.facebook.com创建一个Facebook应用程序并获得APPID和APPSECRET。 (网上有很多教程,所以我会跳过重复一遍)

2)转到:http://developers.facebook.com/tools/explorer并从下拉列表中选择您的应用,然后点击“生成访问令牌”。

3)之后执行以下步骤: https://stackoverflow.com/questions/17197970/facebook-permanent-page-access-token让自己成为永久页面令牌。 (我不能强调这一点,仔细并彻底地遵循这些步骤)*

*我有我为之构建的工具,我输入的是APPID,APPSECRET和ACCESSTOKEN,然后该工具为我生成永久页面令牌。欢迎任何人使用它并帮助改善它,

https://github.com/devfunkd/facebookpagetokengenerator

=============================================== ==========================

好的,此时你应该有 APPID APPSECRET PERMANENT PAGE TOKEN

=============================================== ==========================

在Visual Studio解决方案中:

4)使用Nuget:Install-Package Facebook

5)实施 Facebook客户端

public void PostMessage(string message)
        {
            try
            {
                var fb = new FacebookClient
                {
                    AppId = ConfigurationManager.AppSettings.Get("FacebookAppID"),
                    AppSecret = ConfigurationManager.AppSettings.Get("FacebookAppSecret"),
                    AccessToken = ConfigurationManager.AppSettings.Get("FacebookAccessToken")
                };

                dynamic result = fb.Post("me/feed", new
                {
                    message = message
                });
            }
            catch (Exception exception)
            {
                // Handle your exception
            }
        }  

我希望这有助于任何正在努力解决问题的人。