{"id":4752,"date":"2011-12-18T18:17:13","date_gmt":"2011-12-18T12:47:13","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=4752"},"modified":"2016-12-19T15:16:39","modified_gmt":"2016-12-19T09:46:39","slug":"install-apps-on-facebook-fan-page-using-api","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/install-apps-on-facebook-fan-page-using-api\/","title":{"rendered":"Install Apps on Facebook Fan page using API"},"content":{"rendered":"<p>Hi,<br \/>\nIn 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. <\/p>\n<pre>\r\n\r\n<\/pre>\n<p>But few days ago, Facebook announced that it is <a href=\"http:\/\/developers.facebook.com\/blog\/post\/611\/\"><strong>Removing App Profile Pages<\/strong><\/a>.<br \/>\nNow for adding facebook apps to  fan page, we can either  <strong>Create an Profile page for  app<\/strong> or  <strong>Add the app to Fan page using API<\/strong>.<\/p>\n<pre>\r\n\r\n<\/pre>\n<p>I implemented the feature to attach any Facebook App to Facebook Fan page using API and thought it worth sharing.<\/p>\n<pre>\r\n\r\n<\/pre>\n<p>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 <a href=\"http:\/\/www.tothenew.com\/blog\/integrate-java-application-with-facebook-using-graph-api\/\">here<\/a>.<\/p>\n<pre>\r\n\r\n<\/pre>\n<p>Get App Id from Facebook App page:-<\/p>\n<pre>\r\n\r\n<\/pre>\n<p>To attach any Facebook Fan page, we need to issue an HTTP POST request to the facebook URL.<br \/>\nURL for POST request<br \/>\n[java]<br \/>\nURL :  {FACEBOOK_PAGE_ID}\/tabs<br \/>\n[\/java]<\/p>\n<p>Values we will be requiring :<br \/>\n[java]<\/p>\n<p>String FAN_PAGE_ACCESS_TOKEN = fan page access token.<br \/>\nString APP_ID = app id from the registered app.<br \/>\nString FACEBOOK_PAGE_ID = id of the facebook page to which app is to be attached.<\/p>\n<p>[\/java]<\/p>\n<p>Code to issue post request at the given URL:<br \/>\n[java]<\/p>\n<p>        StringBuilder sb = new StringBuilder(&amp;amp;quot;access_token=&amp;amp;quot;);<br \/>\n        sb.append(URLEncoder.encode(FAN_PAGE_ACCESS_TOKEN, &amp;amp;quot;UTF-8&amp;amp;quot;));<br \/>\n        sb.append(&amp;amp;quot;&amp;amp;amp;app_id=&amp;amp;quot;);<br \/>\n        sb.append(URLEncoder.encode(APP_ID, &amp;amp;quot;UTF-8&amp;amp;quot;));<\/p>\n<p>        URL url = new URL(&amp;amp;quot;https:\/\/graph.facebook.com\/${FACEBOOK_PAGE_ID}\/tabs&amp;amp;quot;);<br \/>\n        HttpURLConnection connection = (HttpURLConnection) url.openConnection();<br \/>\n        String publishedPostId<br \/>\n        try {<br \/>\n            connection.setDoOutput(true);<br \/>\n            connection.setRequestMethod(&amp;amp;quot;POST&amp;amp;quot;);<br \/>\n            connection.setRequestProperty(&amp;amp;quot;Content-Type&amp;amp;quot;, &amp;amp;quot;application\/x-www-form-urlencoded&amp;amp;quot;);<br \/>\n            connection.setRequestProperty(&amp;amp;quot;Content-Length&amp;amp;quot;, &amp;amp;quot;&amp;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            log.debug(&amp;amp;quot;Response code ${connection.responseCode} , Message : ${connection.responseMessage}&amp;amp;quot;)<br \/>\n            if (connection.responseCode != 200) {<br \/>\n                log.debug(&amp;amp;quot;Unable to attach app to facebook fan page&amp;amp;quot;)<br \/>\n            }<br \/>\n        } finally {<br \/>\n            connection?.disconnect()<br \/>\n        }<\/p>\n<p>[\/java]<\/p>\n<pre>\r\n\r\n<\/pre>\n<p>So, we can attach any Facebook App to the Facebook Fan Page using the above code.<\/p>\n<pre>\r\n\r\n<\/pre>\n<p>This worked for me.<br \/>\nHope it helps.<\/p>\n<pre>\r\n\r\n<\/pre>\n<p>Cheers !!!<br \/>\nVishal Sahu<br \/>\nvishal@intelligrape.com<\/p>\n<p><a href=\"http:\/\/www.tothenew.com\">http:\/\/www.intelligrape.com<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&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":1},"categories":[7],"tags":[703,705,707,704,706,700],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/4752"}],"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=4752"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/4752\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=4752"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=4752"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=4752"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}