Hi,
In one of my Grails project, i needed to ‘Like’ any Facebook wall post using facebook API. I searched about it and found a way to achieve this using Facebook Graph API and thought it worth sharing.
To achieve it, we should have a valid facebook access_token, which can be obtained using steps described here.
After obtaining a valid access_token, we can use graph API to like any Facebook Wall post.
We need to have the post_id of the facebook post, which we wants to ‘Like’.
Code to achieve this is as :-
String access_token=ACCESS_TOKEN // access_token obtained from facebook.
String postId=POST_ID // id of the facebook wall post, which is to be 'Liked'.
String feedUrl = "https://graph.facebook.com/${postId}/likes"
StringBuilder sb = new StringBuilder("access_token=");
sb.append(URLEncoder.encode(oAuthToken, "UTF-8"));
URL url = new URL(feedUrl);
HttpURLConnection connection
try {
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "multipart/form-data");
connection.setRequestProperty("Content-Length", "" + sb.toString().length());
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(connection.getOutputStream());
outputStreamWriter.write(sb.toString());
outputStreamWriter.flush();
}
catch (Exception e) {
e.printStackTrace()
}
This issues a POST call for ‘Like’ that particular wall post on facebook.
This worked for me
.
Hope it helps.
Useful Links:
http://developers.facebook.com/docs/reference/api/post/#likes
http://www.intelligrape.com/blog/2010/11/14/integrate-java-application-with-facebook-using-graph-api/
Cheers!!!
Vishal Sahu
vishal@intelligrape.com
http://www.intelligrape.com

