1

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

我正在尝试实现以下代码

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)
                {

                }
            }
}
4

1 回答 1

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

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

好的,此时你应该有你的APPIDAPPSECRET和一个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
            }
        }  

我希望这可以帮助任何正在努力解决这个问题的人。

于 2015-07-13T03:29:25.937 回答