<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>all that jazzfacebook</title>
    <link>http://jazzy.id.au/default/tags/facebook/</link>
    <description>james' blog about java and all that jazz</description>
    <language>en</language>
    <copyright>James Roper</copyright>
    <pubDate>Sat, 12 May 2012 07:02:00 GMT</pubDate>
    <dc:creator>James Roper</dc:creator>
    <dc:date>2012-05-12T07:02:00Z</dc:date>
    <dc:language>en</dc:language>
    <dc:rights>James Roper</dc:rights>
    <image>
      <title>all that jazzfacebook</title>
      <url>http://jazzy.id.au/default/tags/facebook/</url>
    </image>
    <item>
      <title>Facebook OpenID integration in Pebble</title>
      <link>http://jazzy.id.au/default/2010/06/18/facebook_openid_integration_in_pebble.html</link>
      <content:encoded>&lt;p&gt;
I've taken a shot at implementing Facebook OpenID integration into Pebble.  To see the results, at the bottom of this page, click "Add Comment", and then click the Facebook Login link.
&lt;/p&gt;

&lt;p&gt;
Implementing this has been pretty simple.  All the magic happens on the client side, at no point does Pebble make a request on Facebook, and the user is, as far as Pebble is concerned on the server side, never authenticated or trusted in any way.  All this integration does is puts the users name, profile picture and profile link in the author, avatar and website fields of the Pebble comment form.
&lt;/p&gt;

&lt;p&gt;
The bulk of the work has been in modifying pebble to handle profile pictures with comments, and adding a plugin point for this type of plugin.  What follows is a quick tutorial of how to implement a similar OpenID authentication on your site.
&lt;/p&gt;

&lt;h2&gt;1. Create an application on Facebook&lt;/h2&gt;

&lt;p&gt;
Facebook provides some simple instructions on how to do this &lt;a href="http://wiki.developers.facebook.com/index.php/Connect/Setting_Up_Your_Site"&gt;here&lt;/a&gt;.  You will need to create a new application for every different website that you want to integrate with, as Facebook will validate the base URL that it forwards to.
&lt;/p&gt;

&lt;h2&gt;2. Add the login button to your page&lt;/h2&gt;

&lt;p&gt;
There are a number of ways to do this, but the simplest way is to use the Facebook Markup Language (FML).  In the next step we'll configure Facebook to render FML tags, for now, the only thing you need to do is put the following code where you want the login button to appear:
&lt;/p&gt;

&lt;pre name="code" class="html"&gt;
&amp;lt;fb:login-button/&amp;gt;
&lt;/pre&gt;

&lt;h2&gt;3. Initialise the Facebook API&lt;/h2&gt;

&lt;p&gt;
The Facebook client side libraries require that they be initialised.  This is also the stage where Facebook will render any FML tags you've included in the page.  Initialising the Facebook client side API is best done at the bottom of the page, so that it doesn't slow down the loading of the rest of the page.  At this stage you'll need the Facebook application ID from the application you created in step one:
&lt;/p&gt;

&lt;pre name="code" class="html"&gt;
&amp;lt;div id="fb-root"&amp;gt;&amp;lt;/div&amp;gt;
&amp;lt;script src="http://connect.facebook.net/en_US/all.js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script&amp;gt;
    FB.init({appId: "YOUAPPIDHERE", status: false, cookie: true, xfbml: true});
&amp;lt;/script&amp;gt;
&lt;/pre&gt;

&lt;h2&gt;4. Write a function to handle a logged in user&lt;/h2&gt;

&lt;p&gt;
This is where we do most of the work.  The Facebook javascript API provides an event library, and we can use this to update our page with the users details after they log in to Facebook.  This event will only fire when the user logs in or logs out, if a user comes back to your site, and they are already logged in, the event won't fire, even if they click log in, hence we also need to check if the user is already logged in when the page loads.  So we're going to write a function that will handle both of these situations.  The function accepts a Facebook response object, and checks if the response has a session.  This indicates that the user is logged in.  If they are logged in, we make a call on the &lt;tt&gt;/me&lt;/tt&gt; resource of the Facebook Graph API, which will return the users name, link to their profile, and other things.  This function can go anywhere in your code, preferably in a Javascript file along with the rest of your scripts:
&lt;/p&gt;

&lt;pre name="code" class="javascript"&gt;
  function updateFacebookCommentAuthor(response) {
    if (response.session) {
      FB.api("/me", function(response) {
        var avatar = "http://graph.facebook.com/" + response.id + "/picture?type=square";
        document.forms["commentForm"].elements["author"].value = response.name;
        document.forms["commentForm"].elements["website"].value = response.link;
        document.forms["commentForm"].elements["avatar"].value = avatar;
      });
    }
  }
&lt;/pre&gt;

&lt;h2&gt;5. Subscribe to login events&lt;/h2&gt;

&lt;p&gt;
Lastly, we want the above function to be called when a page loads if the user is already logged in, and if a user logs in clicking the login function.  This can either be done at the bottom of the page after the Facebook API is initialised, or it can be done in in the window on load event.  Pebble uses prototype, so I bind it to the window on load event using that:
&lt;/p&gt;

&lt;pre name="code" class="javascript"&gt;
  Event.observe(window, "load", function() {
    FB.Event.subscribe("auth.sessionChange", updateFacebookCommentAuthor);
    FB.getLoginStatus(updateFacebookCommentAuthor);
  });
&lt;/pre&gt;

&lt;p&gt;
This is all you need for basic OpenID authentication using Facebook.  Of course, you might want to, instead of just populating the form, actually render the username and profile picture in the page, as has been done in Pebble.  Another thing you may want to provide is the ability for the user to log out, in case they are using someone else's computer and that person is logged in.
&lt;/p&gt;</content:encoded>
      <category domain="http://jazzy.id.au/default/categories/java/">Java</category>
      <category domain="http://jazzy.id.au/default/tags/facebook/">facebook</category>
      <category domain="http://jazzy.id.au/default/tags/java/">java</category>
      <category domain="http://jazzy.id.au/default/tags/openid/">openid</category>
      <category domain="http://jazzy.id.au/default/tags/pebble/">pebble</category>
      <pubDate>Fri, 18 Jun 2010 12:14:32 GMT</pubDate>
      <guid isPermaLink="false">tag:jazzy.id.au,2010-06-18:default/1276863272737</guid>
      <dc:date>2010-06-18T12:14:32Z</dc:date>
    </item>
    <item>
      <title>Facebook authentication in Java</title>
      <link>http://jazzy.id.au/default/2008/03/19/facebook_authentication_in_java.html</link>
      <content:encoded>&lt;p&gt;If you're a web developer who likes writing practical, quick, and simple utility applications for yourself and others to use, then Facebook is the dream platform.  You don't have to write any user management, sign in, password change pages etc, it comes complete with advanced user management, including friends lists.  There's a high chance that your target users already use it, which makes them more likely to adopt your application because they don't need to go and create yet another username/password on yet another website.  It already has a theme and style sheets, and will present your application surrounded in menus and title bars.  And it provides a large number of ways to publish content and send notifications when events occur.  All these features mean that you, the developer, can spend more time doing what you wanted to do, that is, writing your application.&lt;/p&gt;

&lt;p&gt;If Java is your chosen platform however, you may encounter some difficulties.  Java developers tend to like following well established design patterns.  We like to think of things as beans that have distinct purposes.  We like to use frameworks such as Spring to manage the beans that contain logic, to glue our application together.  We like our data to be stored in beans, and we access that data using getters and setters.  The Facebook client API makes this difficult, for the following reasons:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Its instantiation is dependent on parameters in an HTTP request.  This means we can't use it as a typical Spring bean.&lt;/li&gt;
&lt;li&gt;Depending on which flavour you of client you use, it returns JSON objects, or DOM documents, not the Java beans we like to use.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This article will address the first issue, and in doing so make the second issue less of a worry.&lt;/p&gt;&lt;p&gt;&lt;a href="http://jazzy.id.au/default/2008/03/19/facebook_authentication_in_java.html"&gt;Read more...&lt;/a&gt;&lt;/p&gt;</content:encoded>
      <category domain="http://jazzy.id.au/default/categories/java/">Java</category>
      <category domain="http://jazzy.id.au/default/tags/facebook/">facebook</category>
      <category domain="http://jazzy.id.au/default/tags/java/">java</category>
      <category domain="http://jazzy.id.au/default/tags/spring/">spring</category>
      <pubDate>Wed, 19 Mar 2008 11:48:43 GMT</pubDate>
      <guid isPermaLink="false">tag:jazzy.id.au,2008-03-19:default/1205927323335</guid>
      <dc:date>2008-03-19T11:48:43Z</dc:date>
    </item>
  </channel>
</rss>


