Custom ‘Share’ button on Facebook wall post
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 it.
To implement it, i created my own Share button and thought it worth sharing.
Publishing to facebook wall post requires facebook access_token and we have to do a post call to publish it on facebook wall.
I am assuming that you already have the access_token, if not you can find how to get access_token from here
Basic URL to share somthing on facebook wall is as below
[java]
private static final String SHARE_BUTTON_BASIC_URL = "http://www.facebook.com/sharer.php?u="
private static final String BUTTON_NAME = "Share This" // name to appear on the Share button
[/java]
To create our own ‘Share’ button, we have to oass the custom button parameters in ‘actions’ attribute of the post call.
Code to publish message/post which contains any link attachment is as :-
[java]
String message= //Message to be published on facebook wall
String access_token= // facebook access token
String attachmentImageUrl= // Url of the image to be published in the wall post
String attachmentLink= // Link attached with the message
StringBuilder sb = new StringBuilder("access_token=");
sb.append(URLEncoder.encode(access_token, "UTF-8"));
sb.append("&message=");
sb.append(URLEncoder.encode(message, "UTF-8"));
sb.append("&picture=");
sb.append(URLEncoder.encode(attachmentImageUrl, "UTF-8"));
sb.append("&link=");
sb.append(URLEncoder.encode(attachmentLink, "UTF-8"));
sb.append("&actions=")
sb.append(URLEncoder.encode("[{name: ‘${BUTTON_NAME}’, link: ‘${SHARE_BUTTON_BASIC_URL}${attachmentLink}’ }]", "UTF-8"));
URL url = new URL(‘https://graph.facebook.com/feed’);
HttpURLConnection connection
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", "" + sb.toString().length());
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(connection.getOutputStream());
outputStreamWriter.write(sb.toString());
outputStreamWriter.flush();
connection?.disconnect()
[/java]
So, whatever we provide in the ‘actions’ 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.
Sample post with custom ‘Share This’ link on facebook Wall
This worked in my case.
Hope it helps.
Cheers!!!
Vishal Sahu
vishal@intelligrape.com
www.intelligrape.com