5

I have the following code :

private void CheckAuthorization()
        {
            string app_id = "x";
            string app_secret = "x";
            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('&'))
                    {
                        //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);

                //client.Post("/me/feed", new { message = "A simple test" });

                var args = new Dictionary<string, object>();
                args["message"] = "abc";
                args["caption"] = "This is caption!";
                args["description"] = "This is description!";
                args["name"] = "This is name!";
                args["picture"] = "picutre";
                args["link"] = "http://www.bradspel.net/";

                client.Post("/1418771281723422/feed", args);

            }
        }

When posting I get this :

(OAuthException - #200) (#200) Insufficient permission to post to target on behalf of the viewer

If I change the client.post to this :

client.Post("/me/feed", args);

It works just fine.

So why is this not working when Im about to post to a specific wall? I have set the permission on this facebook page to let everyone post. The facebook app is set to online.

Banshee
  • 15,376
  • 38
  • 128
  • 219

2 Answers2

7

By creating a extended page token and use it to make the post everything works just fine. See this : How to get Page Access Token by code?

Im surprised that this simple task was so hard to get running and that there was vary little help to get.

Community
  • 1
  • 1
Banshee
  • 15,376
  • 38
  • 128
  • 219
  • Me too. Very surprised that this was so convoluted, and the answer was very difficult to find. The small number of votes shows that not many people are doing this. – MagicLAMP Nov 19 '14 at 09:05
0

When posting to a page (as opposed to your own wall), our testing shows that you don't have to Like the brand page to post to it, but you will have to post with viewing permission "Friends" or better (eg Public) in order to avoid this exception. Yes, you'll need your users to grant publish_actions.

robm99x
  • 76
  • 5