Connecting Liferay to Facebook

Welcome to Part 2 of my series on how to connect liferay to social networks. The Last time I showed you how to do it with linkedIn, this time we will take a look into the facebook integration.

Everything that I said about linkedIn also goes for facebook: you need to know your business use cases to know how to integrate it. All I can do is to sho you how to connect to facebook. If you need asisstance, or if you need someone to help you programming in liferay you can book me by sending me an email, or adding me on linkedIn or Skype.

The first step to connect your liferay instance to facebook is to create a developer account. You can do so here: http://developers.facebook.com/. If you need any assistance during your authentication process to facebook you can either ask me or take a look here: http://developers.facebook.com/docs/authentication/.

After you did that, you will have an API key and a secret passcode. That´s like a user / password combination. Add yourself as developer to your app and everybody else who wants to take part in the development.

The next step is to choose a java library that will help you to get the data our of facebook or simply connect your liferay instance of it. I chose restFB, which you can find here: http://restfb.com/.

The general authentication flow is as follows: Redirect the user on his first visit to your app to the authentication URL of facebook and let him accept your app. Then, the flow will return to your servlet, portlet or java application. You can then use the oauth verifier to get the access token from facebook which you can store and reuse.

Redirecting the user

It doesn´t matter, if you use Servlets or portlets as long as you have access to request parameters. if you are using JSF portlets, you can look up how to access request parameters in my blog.

So, the first step will be to redirect the User to facebook to let him accept your app. You can do so by using the following snippet:
String connect = "https://www.facebook.com/dialog/oauth?client_id="
+ Facebook.api_key "&redirect_uri="
+ REDIRECT_URL + "&scope=email,read_stream,offline_access";

Let´s go through this snippet in detail. The first two lines are pretty straight forward: Redirect the User to the facebook oauth page and provide your api key. Then you need to specify a redirect URL. This won´t be a complete URL but instead a path that will be added to the canvas URL you have specified in your facebook developer signup. If you didn´t specify a URL back then, then please do it now. Specify it like http://your-ip and provide the path as redirect URL like /your-facebook-app/your-servlet.
The last thing you have to provide are the rights (= scope) you want to have for your facebook app. All scope types can be found here: http://developers.facebook.com/docs/reference/api/permissions/

The last thing you do is to send the redirect. The User is transported to facebook, needs to accept your app and transported back to your redirect URL.

Get the access token

If the authentication flow returns to you page, you have access to the authentication code:
String code = request.getParameter("code");
This code will be used to get the authentication token. You need to connect to the following URL:
String urlAuthToken= "https://graph.facebook.com/oauth/access_token?client_id="

+ Facebook.api_key+ "&redirect_uri="+ REDIRECT_URL+ "&client_secret="+ Facebook.secret + "&code="+code;
If you perform a GET request to this URL, you will receive the authentication token. This token you need to store and reuse every time your user wants to login  or update some data.
Here is an exemplary call to facebook to get the first- and lastName of a User:
FacebookClient facebookClient = new DefaultFacebookClient("your-authentication-token);
User user = facebookClient.fetchObject("me",User.class);
log.info("Got last name:" + user.getLastName());
That´s all. Now you must decide how to integrate facebook into your liferay portal. If you need help - just contact me by mail (daniel.breitner32@googlemail.com) or skype(daniel_breitner).

If you like this tutorial it would be very nice, if you could click on some of the google ads you see on the right side. It helps me run this block and motivates me ;)

If you have any questions, feel free to leave a comment.