{"id":5928,"date":"2012-06-28T16:12:24","date_gmt":"2012-06-28T10:42:24","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=5928"},"modified":"2015-05-19T17:45:10","modified_gmt":"2015-05-19T12:15:10","slug":"facebook-like-a-wall-post-using-graph-api","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/facebook-like-a-wall-post-using-graph-api\/","title":{"rendered":"Facebook &#8216;Like&#8217; a wall post using Graph API"},"content":{"rendered":"<p>Hi,<br \/>\nIn one of my Grails project, i needed to &#8216;Like&#8217; any Facebook wall post using facebook API. I searched about it and found a way to achieve this using <a href=\"http:\/\/developers.facebook.com\/docs\/reference\/api\/\">Facebook Graph API<\/a> and thought it worth sharing.<\/p>\n<pre><\/pre>\n<p>To achieve it, we should have a valid facebook access_token, which can be obtained using steps described <a href=\"http:\/\/www.tothenew.com\/blog\/integrate-java-application-with-facebook-using-graph-api\/\">here<\/a>.<\/p>\n<pre><\/pre>\n<p>After obtaining a valid access_token, we can use graph API to like any Facebook Wall post.<\/p>\n<pre><\/pre>\n<p>We need to have the <strong>post_id<\/strong> of the facebook post, which we wants to &#8216;Like&#8217;.<\/p>\n<pre><\/pre>\n<p>Code to achieve this is as :-<\/p>\n<p>[java]<br \/>\nString access_token=ACCESS_TOKEN \/\/ access_token obtained from facebook.<br \/>\nString postId=POST_ID \/\/ id of the facebook wall post, which is to be &#8216;Liked&#8217;.<\/p>\n<p> String feedUrl = &quot;https:\/\/graph.facebook.com\/${postId}\/likes&quot;<br \/>\n StringBuilder sb = new StringBuilder(&quot;access_token=&quot;);<br \/>\n sb.append(URLEncoder.encode(oAuthToken, &quot;UTF-8&quot;));<br \/>\n URL url = new URL(feedUrl);<br \/>\n HttpURLConnection connection<br \/>\n try {<br \/>\n connection = (HttpURLConnection) url.openConnection();<br \/>\n connection.setDoOutput(true);<br \/>\n connection.setRequestMethod(&quot;POST&quot;);<br \/>\n connection.setRequestProperty(&quot;Content-Type&quot;, &quot;multipart\/form-data&quot;);<br \/>\n connection.setRequestProperty(&quot;Content-Length&quot;, &quot;&quot; + sb.toString().length());<br \/>\n OutputStreamWriter outputStreamWriter = new OutputStreamWriter(connection.getOutputStream());<br \/>\n outputStreamWriter.write(sb.toString());<br \/>\n outputStreamWriter.flush();<br \/>\n }<br \/>\n catch (Exception e) {<br \/>\n  e.printStackTrace()<br \/>\n }<\/p>\n<p>[\/java]<\/p>\n<pre><\/pre>\n<p>This issues a POST call for &#8216;Like&#8217; that particular wall post on facebook.<\/p>\n<pre><\/pre>\n<p>This worked for me :).<br \/>\nHope it helps.<\/p>\n<pre><\/pre>\n<p>Useful Links:<br \/>\n<a href=\"http:\/\/developers.facebook.com\/docs\/reference\/api\/post\/#likes\">http:\/\/developers.facebook.com\/docs\/reference\/api\/post\/#likes<\/a><br \/>\n<a href=\"http:\/\/www.tothenew.com\/blog\/integrate-java-application-with-facebook-using-graph-api\/\">http:\/\/www.tothenew.com\/blog\/integrate-java-application-with-facebook-using-graph-api\/<\/a><\/p>\n<pre><\/pre>\n<p>Cheers!!!<br \/>\nVishal Sahu<br \/>\nvishal@intelligrape.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, In one of my Grails project, i needed to &#8216;Like&#8217; 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 [&hellip;]<\/p>\n","protected":false},"author":19,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":9},"categories":[7],"tags":[703,705,745,744,4840,704,9,700],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/5928"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/19"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=5928"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/5928\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=5928"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=5928"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=5928"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}