{"id":4938,"date":"2012-01-16T18:35:34","date_gmt":"2012-01-16T13:05:34","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=4938"},"modified":"2016-12-19T15:16:40","modified_gmt":"2016-12-19T09:46:40","slug":"custom-share-button-on-facebook-wall-post","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/custom-share-button-on-facebook-wall-post\/","title":{"rendered":"Custom &#8216;Share&#8217; button on Facebook wall post"},"content":{"rendered":"<p>Hi,<br \/>\nIn one of my Grails project, i needed to have <strong>Share<\/strong> feature on the messages\/post published by our application on facebook wall.<br \/>\nIn Facebook, if the message\/text published is simple text, then <strong>Share<\/strong> link appears on it, but if it contains any link\/URL attached to it, then Facebook do not provides Share button on it.<\/p>\n<pre>\r\n<\/pre>\n<p>To implement it, i created my own <strong>Share<\/strong> button and thought it worth sharing.<\/p>\n<pre>\r\n<\/pre>\n<p>Publishing to facebook wall post requires facebook access_token and we have to do a post call to publish it on facebook wall.<br \/>\nI am assuming that you already have the access_token, if not you can find how to get access_token from <a href=\"http:\/\/www.tothenew.com\/blog\/integrate-java-application-with-facebook-using-graph-api\/\">here<\/a><\/p>\n<pre>\r\n<\/pre>\n<p>Basic URL to share somthing on facebook wall is as below<\/p>\n<p>[java]<\/p>\n<p>    private static final String SHARE_BUTTON_BASIC_URL = &amp;quot;http:\/\/www.facebook.com\/sharer.php?u=&amp;quot;<br \/>\n    private static final String BUTTON_NAME = &amp;quot;Share This&amp;quot; \/\/ name to appear on the Share button<\/p>\n<p>[\/java]<\/p>\n<p>To create our own &#8216;Share&#8217; button, we have to oass the custom button parameters in &#8216;actions&#8217; attribute of the post call.<\/p>\n<p>Code to publish message\/post which contains any link attachment is as :-<\/p>\n<p>[java]<br \/>\n      String message=  \/\/Message to be published on facebook wall<br \/>\n      String access_token= \/\/ facebook access token<br \/>\n      String attachmentImageUrl= \/\/ Url of the image to be published in the wall post<br \/>\n      String attachmentLink= \/\/ Link attached with the message<\/p>\n<p>       StringBuilder sb = new StringBuilder(&amp;quot;access_token=&amp;quot;);<br \/>\n        sb.append(URLEncoder.encode(access_token, &amp;quot;UTF-8&amp;quot;));<br \/>\n        sb.append(&amp;quot;&amp;amp;message=&amp;quot;);<br \/>\n        sb.append(URLEncoder.encode(message, &amp;quot;UTF-8&amp;quot;));<br \/>\n        sb.append(&amp;quot;&amp;amp;picture=&amp;quot;);<br \/>\n        sb.append(URLEncoder.encode(attachmentImageUrl, &amp;quot;UTF-8&amp;quot;));<br \/>\n        sb.append(&amp;quot;&amp;amp;link=&amp;quot;);<br \/>\n        sb.append(URLEncoder.encode(attachmentLink, &amp;quot;UTF-8&amp;quot;));<br \/>\n        sb.append(&amp;quot;&amp;amp;actions=&amp;quot;)<br \/>\n        sb.append(URLEncoder.encode(&amp;quot;[{name: &#8216;${BUTTON_NAME}&#8217;, link: &#8216;${SHARE_BUTTON_BASIC_URL}${attachmentLink}&#8217; }]&amp;quot;, &amp;quot;UTF-8&amp;quot;));<\/p>\n<p>            URL url = new URL(&#8216;https:\/\/graph.facebook.com\/feed&#8217;);<br \/>\n            HttpURLConnection connection<br \/>\n            connection = (HttpURLConnection) url.openConnection();<br \/>\n            connection.setRequestMethod(&amp;quot;POST&amp;quot;);<br \/>\n            connection.setRequestProperty(&amp;quot;Content-Type&amp;quot;, &amp;quot;application\/x-www-form-urlencoded&amp;quot;);<br \/>\n            connection.setRequestProperty(&amp;quot;Content-Length&amp;quot;, &amp;quot;&amp;quot; + sb.toString().length());<br \/>\n            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(connection.getOutputStream());<br \/>\n            outputStreamWriter.write(sb.toString());<br \/>\n            outputStreamWriter.flush();<br \/>\n            connection?.disconnect()<\/p>\n<p>[\/java]<\/p>\n<p>So, whatever we provide in the &#8216;actions&#8217; property of post call, will appear in the wall post with the defined action. Here we are using it as link to Share a facebook wall post.<\/p>\n<pre>\r\n<\/pre>\n<p><strong>Sample post with custom &#8216;Share This&#8217; link on facebook Wall<\/strong><\/p>\n<div style=\"float:left;width:100%\">\n<\/div>\n<pre>\r\n<\/pre>\n<pre>\r\n<\/pre>\n<p>This worked in my case.<br \/>\nHope it helps.<\/p>\n<p>Cheers!!!<\/p>\n<p>Vishal Sahu<br \/>\nvishal@intelligrape.com<br \/>\n<a href=\"http:\/\/www.tothenew.com\">www.intelligrape.com<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, In one of my Grails project, i needed to have Share feature on the messages\/post published by our application on facebook wall. In Facebook, if the message\/text published is simple text, then Share link appears on it, but if it contains any link\/URL attached to it, then Facebook do not provides Share button on [&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":2},"categories":[7],"tags":[703,728,745,744,700],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/4938"}],"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=4938"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/4938\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=4938"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=4938"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=4938"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}