Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I post to a facebook wall from a c# web service?

I've tried a bunch of things and can't figure out how to post content to a facebook wall from a web service. Does anyone know if it's even possible? It seems like the only way to post content is to include a sign-in page within a web site, which isn't what I'd like to do. For twitter, I was able to tweet a message in under 10 lines of code; just add the username/password into a request header and stream the message. I guess what I'm asking is there a similar simple way to do this for facebook? Thanks in advance!

like image 557
Bedantic Avatar asked Oct 14 '22 09:10

Bedantic


1 Answers

You need to create an application on Facebook first. The important settings when setting this up on facebook are:

Connect -> Facebook Connect Settings -> Connect URL

This url serves two purposes. First, that URL will be used as the base for your Cross Domain Communication Channel where you place your xd_receiver.htm file.

So if your the url is: http://yoursite.com then you have to have a file at the url http://yoursite.com/xd_receiver.htm which allows the JavaScript Client Library for Facebook to run.

However, I strongly recommend that you do not use your root url. Choose a subdirectory which you don't link from on your page. Something like http://yoursite.com/ex/fb would do nicely (I like the ex for "external" but it's completely subjective).

While you don't necessarily need the JavaScript Client Library to perform the actual posting. However, you do need it to give yourself the rights you need to post to the wall.

Advanced -> Advanced Settings -> Application Type

This is set to "Desktop".

Once your application is set up, take note of the following things (from the main page of your application):

  1. Your API key
  2. The url of your xd_receiver.htm file

With those two things in hand, you want to create a web page on the site that uses the JavaScript Client Library and FBML to give your application the right to post to its wall (or a page's wall).

In this page, you want to initialize the JavaScript Client Library using your API key as well as the path to your xd_receiver.htm file. I have an ASP.NET page set up like this:

<%@ Page Language="C#"%>
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
  <script 
    src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/
      FeatureLoader.js.php" type="text/javascript"></script>
</head>

<body>
  <fb:login-button></fb:login-button>

  <hr>

  <fb:prompt-permission perms="offline_access">Click to grant offline 
    access permission.</fb:prompt-permission>
  <fb:prompt-permission perms="publish_stream">Click to grant publish 
    stream permission.</fb:prompt-permission>
  <fb:prompt-permission perms="read_stream">Click to grant read stream
    permission.</fb:prompt-permission>

  <input type="button" value="Get session key" onclick="
    document.all('apikey').value=FB.Facebook.apiClient.
    get_session().session_key;" />

  <input type="text" size="100px" id="apikey" />

  <script type="text/javascript">
  FB_RequireFeatures(["XFBML"], function () {
    FB.Facebook.init("API Key", "xd_receiver.htm URL");
  });
</script>

</body>

</html>

The two things of note are:

  • You are requesting the offline_access extended permission. This is key to allowing you to post to the wall of the user when they are not online.
  • The read_stream permission, I don't know if this is necessary to post to the stream, but it doesn't hurt to have.
  • Obviously, the publish_stream permission.

Now, when you go to this page, the JavaScript Client Library will take care of rendering the tags with the fb namespace.

When you first see it, you will see a Facebook Connect button. Click on it and log in using the credentials of the user who's wall you want to post to.

Once you login, the page will be reloaded and the fb:prompt-permission tags will be rendered as links. Clicking on the links tells Facebook that the user allows the application access to those things about their profile.

After you have granted your application all of the permissions (some dialog boxes might appear along the way), click the "Get session key" button.

This will populate the textbox with a session key. Because your application has requested the offline_access, this session key will be recognized by Facebook whenever you use it.

Now, you have the following pieces of information:

  • The application API key
  • The application secret key (which you gain from your application page)
  • The id of the user who's page you want to post to (you should be able to get this from your Facebook url)

At this point, you should be able to use any of the Facebook APIs for .NET. I prefer the Facebook Developer Toolkit.

Create a simple Windows Forms application with your components of choice and se the application API and the application secret key. Also, set the session key to the session key from above before you make the call to login in your Windows Forms app. Set a breakpoint after the login call.

Run your app. A browser window should show up prompting you to login. Once you login, you should hit your breakpoint. At this point, your application secret (if using the Facebook Developer Toolkit) should be different (I call this the client secret. Write this down. This is the fourth, and last piece of information you should need.

Now you have all the pieces you need:

  • Application API
  • Client secret
  • Session key
  • User id of the wall you want to post to

Now, with all of these, when you make your API calls, use the client secret in place of the application secret, as well as your session key, and you shold be able to make calls to write to the wall of the user, (or a page that the user owns, if you have that id) from your service, without any UI logins.

like image 169
casperOne Avatar answered Nov 15 '22 04:11

casperOne