Sudhanshu Tiwari
Sudhanshu Tiwari

Reputation: 1

how to post on directly at facebook page using C# not at visitor post?

i refereed this post how to post to facebook page wall from .NET but it will work on facebook page to post as visitor post. and i want to post of page timeline not at visitor post and i am admin of this page and app. please give solution for it .

here in my code

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+" ." });



        }

    }

}

Upvotes: 0

Views: 4587

Answers (1)

andyrandy
andyrandy

Reputation: 74014

You need to use a Page Token, not a User Token. Make sure it really is a Page Token by debugging it: https://developers.facebook.com/tools/debug/accesstoken/

Also, publish_actions is the wrong permission to post as Page, you need to use publish_pages.

Additional information:

Upvotes: 2

Related Questions