Facebook Connect Plugin

  • Tags: security, facebook
  • Latest: 0.2
  • Last Updated: 01 August 2010
  • Grails version: 1.3.3 > *
  • Authors: null
6 votes
Dependency:
compile ":facebook-connect:0.2"

 Documentation

Summary

Installation

To install the Facebook Connect plugin type this command from your project's root folder:

grails install-plugin facebook-connect

Description

Facebook Connect Plugin

This plugin provides simple authentication using Facebook Connect. Using Facebook Connect you don't need to store user credentials in your own application, so no registration, forget password, confirmation or other flows need to be implemented anymore.

Facebook Connect is the next evolution of Facebook Platform - enabling you to integrate the power of Facebook Platform into your own site. Enable your users to…

  • Seamlessly "connect" their Facebook account and information with your site
  • Connect and find their friends who also use your site
  • Share information and actions on your site with their friends on Facebook
The plugin contains:
  • FacebookConnectService: This server validates a user' s facebook credentials. It also provides you a Facebook Json Client so that you can call remote facebook methods on the server side.
  • FacebookConnectTaglib: This provides a shortcut for you to add all the necessary facebook html tags into your pages.More facebook tags can be found at http://wiki.developers.facebook.com/index.php/XFBML and
  • facebook-java-api: http://code.google.com/p/facebook-java-api/ This library provides the Facebook Json Client that is initialized by the FacebookConnectService. Please follow the url to be more familiar with all the provided features.
Please create issues in JIRA if you encounter problems.

Installation

To install the Facebook Connect plugin type this command from your project's root folder:

grails install-plugin facebook-connect

Basic Setup

  1. Install the plugin
  2. If you dont already have a facebook account signup for facebook. Login.
  3. Go to the developer app http://www.facebook.com/developers/
  4. If you don't already have a Facebook Platform API key for your site, create an application with the Facebook Developer application by going to http://www.facebook.com/developers/editapp.php. If you already have an application skip to step 11.
  5. Enter a name for your application in the Application Name field.
  6. Click the Optional Fields link to see more entry fields.
  7. Keep all of the defaults, except enter a Callback URL. This URL should point to the top-level directory of the site which will be implementing Facebook Connect (this is usually your domain, e.g. http://www.example.com, but could also be a subdirectory).
  8. If your site is going to implement Facebook Connect across a number of subdomains of your site (for example, foo.example.com and bar.example.com), you need to enter a Base Domain (which would be example.com in this case). Specifying a base domain allows you to make calls using the Grails and JavaScript client libraries as well as get and store session information for any subdomain of the base domain.
  9. You should include a logo that appears on the Facebook Connect dialog. Next to Facebook Connect Logo, click Change your Facebook Connect logo and browse to an image file. The logo can be up to 99 pixels wide by 22 pixels tall, and must be in JPG, GIF, or PNG format.
  10. Click Submit to save your changes.
  11. Take note of the API Key and secret key.
  12. You'll see a new file called FacebookConnectConfig.groovy added in grails-app/conf directory. Open this file and write your API key and secret key for your facebook app.
  13. Open the view layout that you are planning to use for your facebook connect enabled pages.
  14. At the very bottom, right before
</body>
write
<g:facebookConnectJavascript  />
This will enable all the facebook markup language tags(fbml) and facebook connect login. if your site uses ssl you should instead write
<g:facebookConnectJavascript secure="true" />
if you would like to provide a base url, you can write it as
<g:facebookConnectJavascript base="http://www.example.com" />
  1. On the top of the page, change your html tag and docType to
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">

Sample Page

After these following the steps above, you can create a simple page like

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<body>

<fb:login-button autologoutlink="true"></fb:login-button> <br/> <fb:name uid="loggedinuser" useyou="false"></fb:name> <fb:profile-pic uid="loggedinuser" size="normal" />

<g:facebookConnectJavascript /> </body> </html>

This should give you a facebook login button. After you login, it should show you your name, and picture. You can find out all the facebook apis and facebook json client apis from the links provided in the plug-in contents section.

How to make Facebook Connect a part of your authentication?

The plugin also contains FacebookConnectService which can be used in custom application services or taglibs. It can be injected as any other Grails service:

class MyService {
    def facebookConnectService

… }

FacebookConnectService provides you 3 methods.

  • isLoggedIn(HttpServletRequest request)
You should call this method as
facebookConnectService.isLoggedIn(request)
in your controller, or authentication method, and pass in the httpServletRequest object.In return, you will get a boolean response, to tell you if the user is logged in and authenticated as a facebook user or not.
  • getFacebookClient()
You can call this method after you have verified that the user is authenticated and logged in with the above method. Otherwise, you'll likely to get a facebook exception about facebook session. In return, you'll get a facebookJsonClient that you can use for almost everything facebook related. This client's api is provided in the url that is mentioned in the plug in contents section.
  • isValidForSignup(Map params)
You should call this method as
facebookConnectService.isValidForSignup(params)
in your controller. This method should used if you have setup your facebook application to post you a form whenever a new user has added your application for the first time and you need to validate posted parameters. More information about this is provided at http://wiki.developers.facebook.com/index.php/Post-Authorize_URL

How to get some data to store in our database to identify the user?

The facebook library that the plugin uses can handle pretty much everything that facebook api provides. By using that library you can query facebook for any information, and use/store it however you need it to. This includes the facebook user id. Ofcourse, there is the facebook terms and conditions, but that's beyond the plugin :)