Facebook ‘Like’ a wall post using Graph API
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 :-
[java]
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()
}
[/java]
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.tothenew.com/blog/integrate-java-application-with-facebook-using-graph-api/
Cheers!!!
Vishal Sahu
vishal@intelligrape.com
Thanks for your posting.
I don’t understand about “oAuthToken” in line
sb.append(URLEncoder.encode(oAuthToken, “UTF-8”));
Could you give me more detail ?
I like the valuable info you supply for your articles.
I will bookmark your blog and take a look at
again right here regularly. I am reasonably sure I’ll be told many
new stuff right here! Good luck for the next!
Nice response in return of this issue with solid arguments and explaining everything regarding
that.
i have put last 3 posts in web page , if user loged in he can add comment / like , problem is if user loged in and try add like or comment he is getting an permission error #200 , if i loged in i can add like or comment (application is for me) , am getting the all permission nedded from the user , so how can i give him permission to add like / comment , if you would like i can send you the code . thanks for interest .
Hi, can you please tell me the exact problem you are facing.
can you help me , am trying amplement same way but using php