0

Does anyone have a definitive way to post to a user's wall, using nothing but the .NET Framework, or Silverlight?

Problems deriving from people's attempts have been asked here on SO, but I cannot find a full, clear explanation of the Graph API spec and a simple example using WebClient or some similar class from System.Net.

Do I have to send all feed item properties as parameters in the query string? Can I construct a JSON object to represent the feed item and send that (with the access token as the only parameter)?

I expect its no more than a 5 line code snippet, else, point me at the spec in the FB docs.

Thanks for your help,

Luke

Luke Puplett
  • 42,091
  • 47
  • 181
  • 266

2 Answers2

1

I don't know much about .net or silverlight, but the facebook api works with simple http requests. All the different sdks (with the exception of the javascript one) are mainly just wrappers for the http requests with the "feature" of adding the access token to all requests.

Not in all requests the parameters are sent as querystring, in some POST requests you need to send them in the request body (application/x-www-form-urlencoded), and you can not send the data as json.

If the C# sdk is not to your liking, you can simply create one for your exact needs. As I wrote, you just need to wrap the requests, and you can of course have a method that will get a json as parameter and will break it to the different parameters to be sent along with the request.

I would point you to the facebook documentation but you haven't asked anything specific so there's nothing to point you to except for the landing page.

Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299
  • Thanks for your response. The interesting part, as seen in rlm2's answer, is the body encoding is not JSON! Thanks for this. – Luke Puplett May 08 '12 at 19:32
1

This is taken from how we post to a user's wall. We place the data for the post in the request body (I think we found this to be more reliable than including all the parameters in the query part of the request), it has the same format as a URL encoded query string.

I agree that the documentation is rather poor at explaining how to interact with a lot of resources. Typically I look at the documentation for information on fields and connections, then work with the Graph API Explorer to understand how the request needs to be constructed. Once I've got that down it's pretty easy to implement in C# or whatever. The only SDK I use is Facebook's Javascript SDK. I've found the others (especially 3rd party) are more complicated, buggy, or broken than rolling my own.

private void PostStatus (string accessToken, string userId)
{
    UriBuilder address = new UriBuilder ();
    address.Scheme = "https";
    address.Host = "graph.facebook.com";
    address.Path = userId + "/feed";
    address.Query = "access_token=" + accessToken;

    StringBuilder data = new StringBuilder ();
    data.Append ("caption=" + HttpUtility.UrlEncodeUnicode ("Set by app to describe the app."));
    data.Append ("&link=" + HttpUtility.UrlEncodeUnicode ("http://example.com/some_resource_to_go_to_when_clicked"));
    data.Append ("&description=" + HttpUtility.UrlEncodeUnicode ("Message set by user."));
    data.Append ("&name=" + HttpUtility.UrlEncodeUnicode ("App. name"));
    data.Append ("&picture=" + HttpUtility.UrlEncodeUnicode ("http://example.com/image.jpg"));

    WebClient client = new WebClient ();
    string response = client.UploadString (address.ToString (), data.ToString ());
}
  • Thank you. This is exactly what I was after. I think REST is so simple that I really don't want SDKs or abstractions as another dependency, technical and mental. – Luke Puplett May 08 '12 at 19:33
  • And 46 seconds later, I have posted something! SO now has a definitive example for posting to a user's wall using just .NET. Good work. – Luke Puplett May 08 '12 at 19:36
  • Slight hitch, the post is only visible to me. So it turns out this is a FB default and user's have to set it otherwise, which won't happen, so this was a day wasted! LOL. – Luke Puplett May 08 '12 at 19:43
  • As I recall when a user first accepts the permissions for an app they get the chance to set the default "visibility" for posts from an app. Also I think that when the user changes the visibility when approving a post that it is "sticky" and is used by default for future posts. –  May 08 '12 at 20:32
  • Thanks for the info. I guess an app posting to a user's wall is supposed to be a feed for the user, to keep them engaged with the product, such as periodic recommendations, news etc. I was trying to use the wall as a way to publicise my app to my customer's friends. – Luke Puplett May 08 '12 at 21:05
  • It's probably doing what you want, default visibility is to make posts visible to all and I suspect very few users change that. Our app posts to the user's wall when they create something and allows a user to like the app; those both work well. You may know this already but when an app you are using posts to your wall or you like something you don't see it on your wall, but your friends do (if it's visible to them). Your own activity only shows up in your timeline now, which we found confusing at first. –  May 08 '12 at 21:43
  • I see. I'll have another go with another FB account and see. I heard that new apps are set to private by default these days, but I'll check. – Luke Puplett May 09 '12 at 08:11