al-web
al-web

Reputation: 77

Get facebook page token

As off right now i'm creating a post from my private account and on my page. I'm trying to get the page it self to post the message i have created. I been reading som documentation about page tokens. But finding it difficult to find some eksampels. Does anyone of you guys know the next step to getting to post messages as a page?? Would be greatly thankful

public void CheckAutorization()
    {
        string app_Id = "my_app_id";
        string app_secret = "my_secret_id";
        string scope = "publish_stream, publish_actions";

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




            dynamic parameters = new ExpandoObject();
            parameters.message = "test message";
            parameters.link = "http://www.facebook.com";
            parameters.picture = "http://www.fabric.co.uk/wp-content/uploads/2014/06/Facebook-teen-usage.jpg";
            parameters.name = "facebook.fo";

            parameters.caption = "test caption";
            client.Post("/mypageUrl/feed", parameters);
        }
    }

Upvotes: 1

Views: 1846

Answers (1)

andyrandy
andyrandy

Reputation: 73984

What you need to post as a Page is a Page Access Token. Take a look at the Facebook docs about how to get it: https://developers.facebook.com/docs/facebook-login/access-tokens

Basically, you just call /me/accounts to get Access Tokens for all Pages and you use the particular Token to post instead of the User Token.

Here´s an article how to create an Extended Page Access Token without using the PHP SDK: http://www.devils-heaven.com/extended-page-access-tokens-curl/

Upvotes: 1

Related Questions