如何使用C#直接在Facebook页面上发帖而不是访客帖子?

时间:2016-04-30 06:35:10

标签: c# asp.net facebook facebook-graph-api facebook-c#-sdk

我对这篇文章进行了评论 how to post to facebook page wall from .NET但它可以在Facebook页面上发布作为访客帖子发布。 我想发布页面时间线不在访客帖子,我是这个页面和应用程序的管理员。 请给它解决方案。

这里是我的代码

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Facebook;
using System.Dynamic;

public partial class facebook : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        authontication();
    }
    private void authontication()
    {
        string app_id = "**********************";
        string app_secret = "###################################";


        string scope = "publish_actions,manage_pages";

        if (Request["code"] == null)
        {
            Response.Redirect(string.Format(
                "https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}&scope={2}",
                app_id, 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}",
            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('&'))
                {
                    //meh.aspx?token1=steve&token2=jake&...
                    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();
            string name = "sudhanshu";

            name += " new data";

            client.Post("/1168908853153698/feed", new { message = "My first appProduct "+name+" ." });



        }

    }

}

1 个答案:

答案 0 :(得分:1)

您需要使用页面令牌,而不是用户令牌。通过调试它确保它确实是一个页面令牌:https://developers.facebook.com/tools/debug/accesstoken/

此外,publish_actions是以网页形式发布的错误权限,您需要使用publish_pages

其他信息: