Tuesday, July 26, 2011

Sending Email Using PostMark

Having your webapp send out emails, is well, according to me, a tricky issue. Its definitely a convenience for your users, as long as, there is no delay in receiving the mail. But beware, that delay can make or break the functionality!

When we first tried using the SMTP server of CityNaksha, we were happy for the functionality, but absolutely disheartened with the mail delivery! After a while, the delays were piling up to a couple of hours! It was completely unacceptable.

A couple of days and a question on StackOverflow later, Postmark came to the rescue! And well, till now, we are happy with the result. Here's a quick primer, compiled from the notes that I was jotting down, while implementing Postmark for out webapp.

We run on Java, with Struts.
  1. Signup on Postmark and create an account there. They have a package of 1,000 free emails to help you set things up
  2. There are a host of plugins you can choose from. For Java, we used Jared Holdcroft's library
  3. I couldn't locate a combined jar file in there, so had to put all the source files in my project, and the associated jar's in the build path of the project and the WEB-INF/lib location
  4. The code snippet from the test file, is quite helpful
We have been using it for a few days now, and are quite happy with the performance. The integration didn't take much time either.

Overall rating : 4/5 stars

Lets keep connecting the city.
RD,
Facilitator, CityNaksha.

Thursday, April 7, 2011

Post On Facebook Page As Page Not As Admin User

Sometimes, even the seemingly easy things, can actually end up being very tough. And that is what happened, when I tried to implement auto updation on my Facebook page, from my website. So, after I finally got it done, I decided to write about it here. Partly for future reference, and partly, out of an altruistic urge! :)

Before we proceed, here's my setup :
Webserver : Apache Tomcat
Framework : Java Struts
FB Utility API : restfb

And here's my problem :
I have a facebook page pageabc. I have a facebook user user123, who has administrative privileges for the page. I want to post an update on pageabc, as pageabc (not as user123). And I want this to happen, as a trigger from my webapp.

If you have not used restfb before, then I highly recommend you go through my friend Saurav's blogpost on a simple application to post updates on your Facebook profile. Its well written, it will help you set yourself up for the task, and above all, I will get paid for directing traffic to his blog! ;)

Now, if you can post a status update to your account, you are all set to move on from here. Just create a page on Facebook, set yourself up as an admin to the page and start making these changes to your code.

First up, the permissions need to be changed. For posting to a page, as a page, you need two permissions.
  • manage_pages : This will give you permission to access the data related to a user's pages
  • publish_stream : This is a basic permission, needed to update facebook status message
This information will go into your Authorize URL, and will look something like this : https://graph.facebook.com/oauth/authorize?client_id=**************&scope=manage_pages,publish_stream&redirect_uri=

Next, you need to get the pages managed by the user. The access token that Facebook provides you now, will let you access a user's pages, as well as publish to the user's feed. This can be obtained from restfb in this way :

DefaultFacebookClient fbClient;
Connection myAccounts;
fbClient = new DefaultFacebookClient(initialAccessToken);
myAccounts = fbClient.fetchConnection("me/accounts", Account.class);
You now need to parse through myAccounts, to get the names of the different pages that the user is associated with. Iterate through it, till you get the desired page. Each page will also have its own Access Token, which is associated with the permissions allowed on that page. Once you have iterated through to the page you want, get its corresponding access token (call it, pageAccessToken). This will now be used to post to the page, as the page itself.

fbClient = new DefaultFacebookClient(pageAccessToken);
fbClient.publish("me/feed", FacebookType.class, Parameter.with("message", "Aloha! ;)"));
And voila! That's it. Aloha! ;) will get posted to the page, not as an update from user123, but as an update from pageabc. The message posted this way, will successfully propagate to all the fans of the page (people who have Liked the particular page)

In conclusion :
I spent more than a couple of evenings, figuring this whole thing out. It was quite simple, but I am not very proficient in using public API's. So, it took me a little while, to get the solution. If you really liked this, feel free to send me some money over! ;) And if you really, REALLY like it, just drop in a line in the comments section!

Lets keep connecting the city.
RD,
Facilitator, CityNaksha.

Some helpful links :
http://developers.facebook.com/docs/reference/api/
http://restfb.com/
http://www.masteringapi.com/tutorials/how-to-post-on-facebook-page-as-page-not-as-admin-user-using-php-sdk/31/
http://codifyit.blogspot.com/2010/12/integrate-your-web-site-with-facebook.html