Facebook Fan Page « Intelligrape Groovy & Grails Blogs

Posts Tagged ‘ Facebook Fan Page ’

Facebook ‘Like’ a wall post using Graph API

Posted by on June 28th, 2012

Hi,
In one of my Grails project, i needed to ‘Like’ 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 here.


After obtaining a valid access_token, we can use graph API to like any Facebook Wall post.


We need to have the post_id of the facebook post, which we wants to ‘Like’.


Code to achieve this is as :-

String access_token=ACCESS_TOKEN // access_token obtained from facebook.
String postId=POST_ID // id of the facebook wall post, which is to be 'Liked'.

 String feedUrl = "https://graph.facebook.com/${postId}/likes"
 StringBuilder sb = new StringBuilder("access_token=");
 sb.append(URLEncoder.encode(oAuthToken, "UTF-8"));
 URL url = new URL(feedUrl);
 HttpURLConnection connection
 try {
 connection = (HttpURLConnection) url.openConnection();
 connection.setDoOutput(true);
 connection.setRequestMethod("POST");
 connection.setRequestProperty("Content-Type", "multipart/form-data");
 connection.setRequestProperty("Content-Length", "" + sb.toString().length());
 OutputStreamWriter outputStreamWriter = new OutputStreamWriter(connection.getOutputStream());
 outputStreamWriter.write(sb.toString());
 outputStreamWriter.flush();
 }
 catch (Exception e) {
  e.printStackTrace()
 }


This issues a POST call for ‘Like’ that particular wall post on facebook.



This worked for me :) .
Hope it helps.



Useful Links:
http://developers.facebook.com/docs/reference/api/post/#likes
http://www.intelligrape.com/blog/2010/11/14/integrate-java-application-with-facebook-using-graph-api/


Cheers!!!
Vishal Sahu
vishal@intelligrape.com
http://www.intelligrape.com

Posted in Grails

Posting photos to Facebook album using Graph API

Posted by on June 28th, 2012

Hi,
In my recent grails project, i needed to post images to the albums for Facebook Profile/ Fan page/ Group using facebook API. I searched a lot and find a way to achieve this using Facebook Graph API.



To post any content over facebook, we need to have facebook access_token which can be obtained using steps described here



After successfully obtaining access_token, we can easily use graph API to post message/photos to any album for that profile or page.
We need to have album_id to post photos to any album. If there is no album present, the it will create an album with the same name as of the Facebook App, which is posting the photos and put all the photos in that Album.



Make sure the access_token have publish_stream permission to post the content over facebook.



Code to post photos to facebook album is as :-





String access_token= ACCESS_TOKEN // access_token obtained from facebook
String message= MESSAGE // message to be published with photo.
String photo_url=PHOTO_URL // url of the photo to be published

StringBuilder sb = new StringBuilder("access_token=");
sb.append(URLEncoder.encode(token, "UTF-8"));
sb.append("&message=");
sb.append(URLEncoder.encode(message, "UTF-8"));
sb.append("&url=");
 sb.append(URLEncoder.encode(photo_url, "UTF-8"));
 String feedUrl = "https://graph.facebook.com/me/photos" 
// use Album ID or FanPage ID in the feedURL instead of  /me/ in case you want to publish photos to specific Album or Fan page

 URL url = new URL(feedUrl);
 HttpURLConnection connection
 try {
  connection = (HttpURLConnection) url.openConnection();
  connection.setDoOutput(true);
  connection.setRequestMethod("POST");
  connection.setRequestProperty("Content-Type", "multipart/form-data");
  connection.setRequestProperty("Content-Length", "" + sb.toString().length());
  OutputStreamWriter outputStreamWriter = new OutputStreamWriter(connection.getOutputStream());
  outputStreamWriter.write(sb.toString());
  outputStreamWriter.flush();
   }
  catch (Exception e) {
     e.printStackTrace()
   }



This will post the provided image via url to the album mentioned in the API call.



This worked for me.
Hope it helps.



Useful Links:
http://developers.facebook.com/docs/reference/api/album/
http://www.intelligrape.com/blog/2010/11/14/integrate-java-application-with-facebook-using-graph-api/


Cheers!!!
Vishal Sahu
vishal@intelligrape.com
http://www.intelligrape.com

Posted in Grails

Way to check if user has “Liked” the Facebook Fan Page or Not

Posted by on December 22nd, 2011

Hi,
In my recent grails project, i was working on creating Apps for Facebook Fan Page and needed to show data to the user in such a way so that if he/she has “Liked” the page, then the content would be different from the case when the user has not liked it.


In simple words, if user is Fan of any Facebook Page, then the content of App Tab would be different from the user who is not Fan of that Page.


I searched a lot about it, and came across various solutions suggesting using JavaScript way to handle it but they didn’t worked for me, because although the User is logged in the facebook, but has not authorized the App and hence, we cannot fetch user_id of that user from facebook session.


The only way i left with is to fetch the User status from the signed_request served in the params. This is the easiest way i got, to check whether User has ‘Liked’ the page or not, on server side.


A signed_request is passed in params to the application URL registered in App on Facebook when they are loaded into the Facebook environment. It is of the form of long sequence of string concatenation of a HMAC SHA-256 signature string, a period (.), and a base64url encoded JSON object.


Code to decode signed_request


        String signedRequest= SIGNED_REQUEST_OBTAINED_IN_PARAMS
        boolean hasLikedPage = false
        String payload = signedRequest.split("[.]", 2)[1]
        payload = payload.replace("-", "+").replace("_", "/").trim()
        String jsonString = new String(Base64.decodeBase64(payload.getBytes()))
      

The above code will gives a string containing all the data in the signed_request(as in below example).
Example:

{ 
   "algorithm":"HMAC- SHA256", 
    "expires":1324555200,
    "issued_at":1324551348,
    "oauth_token":"some-random-string-of-oauth-token",
    "page": { 
            "id":"XXXXXXXXXXX594",
             "liked":true, // field to decide if user has "Liked" the fan page or not
             "admin":false
          },
    "user":{ 
            "country":"in",
            "locale":"en_US",
            "age":{
                     "min":21
                    }
              },
    "user_id":"XXXXXXX3285"
}

With the above JSON, we can see if the user has “Liked” the Fan Page or Not.


This worked for me.
Hope it helps



Cheers!!
Vishal Sahu
vishal@intelligrape.com
http://www.intelligrape.com
http://www.linkedin.com/in/vishalsahu

Posted in Grails

Install Apps on Facebook Fan page using API

Posted by on December 18th, 2011

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

URL :  https://graph.facebook.com/${FACEBOOK_PAGE_ID}/tabs

Values we will be requiring :


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.

Code to issue post request at the given URL:


        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()
        }


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
http://www.linkedin.com/in/vishalsahu
http://www.intelligrape.com

Posted in Grails, Groovy