3

I have created a facebook page and a facebook application for my website and now I need to post messages onto the facebook page with help of facebook SDK .NET.

This is what I got so far :

public static bool UploadPost(string message)
    {
        dynamic result;

        //https://developers.facebook.com/tools/explorer/
        //https://developers.facebook.com/tools/access_token/
        FacebookClient client = new FacebookClient("secret access token");


        result = client.Get("oauth/access_token", new
            {
                client_id = "[Client ID number]",
                client_secret = "[Client sercret",
                grant_type = "client_credentials",
            });

        result = client.Post("[facebook app Id]/feed", new { message = "Test Message from app" });
        //result.id;
        result = client.Get("[facebook app Id]");

        return false;
    }

When running this I get : Additional information: (OAuthException - #200) (#200) The user hasn't authorized the application to perform this action on client.Post. If I remove the client.Post row every thing works good, the correct data is fetched.

I have tried follow some helps on facebook SDK .NET website but it is still not working.

The main problem now is that I get permission exception. I was hoping that my facebook app hade enouth permissions to publish post from my website to the facebook page.

Banshee
  • 15,376
  • 38
  • 128
  • 219
  • Did you register your application with Facebook? Tutorial at https://developers.facebook.com/docs/web/tutorials/scrumptious/register-facebook-application/. Did you request the correct type of permission from the User to post to their wall? – Matthew at Critical Cognition May 19 '14 at 22:19

4 Answers4

3

Here is a step wise tutorial to register your application with facebook and get an app Id for your application.

Then for permissions ::

    private const string ExtendedPermissions = "user_about_me,read_stream,publish_stream";

This is a string of permissions. Pass it on further for getting correct permissions to post messages on page. Post using your standard code for posting no FB pages. Cheers. Hope it helps.

Gaurav Deochakke
  • 2,265
  • 2
  • 21
  • 26
0

Are you trying to post to [facebook app id]?

I would recomend to post to "me/feed" and test if that works.

Also, to post to Facebook you have to have the publish_stream permission

private async Task Authenticate()
{
    string message = String.Empty;
    try
    {
        session = await App.FacebookSessionClient.LoginAsync("user_about_me,read_stream,publish_actions");
        App.AccessToken = session.AccessToken;
        App.FacebookId = session.FacebookId;

        Dispatcher.BeginInvoke(() => NavigationService.Navigate(new Uri("/Pages/LandingPage.xaml", UriKind.Relative)));
    }
    catch (InvalidOperationException e)
    {
        message = "Login failed! Exception details: " + e.Message;
        MessageBox.Show(message);
    }
}

Should work :)

Daniel Steiner
  • 510
  • 1
  • 8
  • 23
  • I tried me but that does not work. I read somthing about that this was couse I was posting as the app and not as a user. How do I get the publish_stream permission? – Banshee May 14 '14 at 10:26
  • In case you are using the "official .NET SDK" for Facebook, see my edit. – Daniel Steiner May 14 '14 at 10:30
  • Official? Im using this http://facebooksdk.net/ where you creates a FacebookClient, do I need more libraries? – Banshee May 14 '14 at 10:59
  • Not much with your sample works, maybe partly due to me running .NET 4.0. But the App and FacebookSessionClient is is not found? where do you get that from? And why complicate it with async and await? Also, it looks like you are navigating to a webpage? All I want to do is to use the Facebook app to publish some messages on to the Facebook page. – Banshee May 14 '14 at 17:08
  • That sample is from the Project Site, tough it was for a Widnows Phone Project. – Daniel Steiner May 15 '14 at 13:26
  • Okay, that is apparently not the same as the web project. I dont got a FacebookSessionClient in my facebook SDK.net. – Banshee May 16 '14 at 19:09
  • Hmm I can try to find something in the documentation for you, if you still need help with this. – Daniel Steiner May 26 '14 at 09:10
  • I do, it seems to be a bit hard to get it running. – Banshee Jun 02 '14 at 08:06
0

The following should work.

var fb = new FacebookClient("access_token");

fb.PostCompleted += (o, e) => {
    if(e.Error == null) {
        var result = (IDictionary<string, object>)e.GetResultData();
        var newPostId = (string)result.id;
    }
};

var parameters = new Dictionary<string, object>();
parameters["message"] = "My first wall post using Facebook SDK for .NET";
fb.PostAsync("me/feed", parameters);

This was taken directly from the documentation.

Igor Ševo
  • 5,459
  • 3
  • 35
  • 80
  • You are not rquesting any permissions? All you do diffrent then I do is that you use PostAsync instead of regular Post and that should not matter. – Banshee May 15 '14 at 11:02
  • 1
    I dont think the problem is code, I suspect that something needs to be set in the app that my website uses to post with. Becides, me/feed will never work when using facebook app. – Banshee May 16 '14 at 19:08
0

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