Install Apps on Facebook Fan page using API
Hi,
In my current grails project, we are using Facebooks Apps for our application so that any user who wish to use that app, can attach it with his/her Facebook Fan Page manually by visiting the App Profile Page and then adding it to the Fan Page. Then the attached facebook app can fetch data from our project.
But few days ago, Facebook announced that it is Removing App Profile Pages.
Now for adding facebook apps to fan page, we can either Create an Profile page for app or Add the app to Fan page using API.
I implemented the feature to attach any Facebook App to Facebook Fan page using API and thought it worth sharing.
I am assuming that you already have the Facebook Page Access Token with you, if not you can see how to Integrate facebook with web application and get access token here.
Get App Id from Facebook App page:-
To attach any Facebook Fan page, we need to issue an HTTP POST request to the facebook URL.
URL for POST request
[java]
URL : {FACEBOOK_PAGE_ID}/tabs
[/java]
Values we will be requiring :
[java]
String FAN_PAGE_ACCESS_TOKEN = fan page access token.
String APP_ID = app id from the registered app.
String FACEBOOK_PAGE_ID = id of the facebook page to which app is to be attached.
[/java]
Code to issue post request at the given URL:
[java]
StringBuilder sb = new StringBuilder("access_token=");
sb.append(URLEncoder.encode(FAN_PAGE_ACCESS_TOKEN, "UTF-8"));
sb.append("&app_id=");
sb.append(URLEncoder.encode(APP_ID, "UTF-8"));
URL url = new URL("https://graph.facebook.com/${FACEBOOK_PAGE_ID}/tabs");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
String publishedPostId
try {
connection.setDoOutput(true);
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();
log.debug("Response code ${connection.responseCode} , Message : ${connection.responseMessage}")
if (connection.responseCode != 200) {
log.debug("Unable to attach app to facebook fan page")
}
} finally {
connection?.disconnect()
}
[/java]
So, we can attach any Facebook App to the Facebook Fan Page using the above code.
This worked for me.
Hope it helps.
Cheers !!!
Vishal Sahu
vishal@intelligrape.com
Awesome things here. I’m very satisfied to look your post. Thank you so much and I am having a look ahead to contact you. Will you please drop me a mail?