<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Claudio&#039;s Hideout &#187; how to</title>
	<atom:link href="http://claudio.cicali.name/post/tag/how-to/feed/" rel="self" type="application/rss+xml" />
	<link>http://claudio.cicali.name</link>
	<description>Claudio Cicali web hub</description>
	<lastBuildDate>Fri, 30 Jul 2010 14:03:19 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Use Facebook Connect in your application</title>
		<link>http://claudio.cicali.name/post/2010/01/use-facebook-connect-in-your-application/</link>
		<comments>http://claudio.cicali.name/post/2010/01/use-facebook-connect-in-your-application/#comments</comments>
		<pubDate>Mon, 25 Jan 2010 08:00:12 +0000</pubDate>
		<dc:creator>claudio</dc:creator>
				<category><![CDATA[Articoli]]></category>
		<category><![CDATA[Programmazione]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[connect]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://claudio.cicali.name/?p=615</guid>
		<description><![CDATA[Recently I&#8217;ve got to enable one site of mine with this &#8220;Facebook Connect&#8221; thingy, my primary goal being the increase of the conversion rate, in the way that people could use their Facebook account to log into my web site without a need for a boring registration process. Just click two buttons, and you&#8217;re in. &#8230; <span class='readmore'>[<a href='http://claudio.cicali.name/post/2010/01/use-facebook-connect-in-your-application/'>continua &#187;</a>]</span>]]></description>
			<content:encoded><![CDATA[<p>Recently I&#8217;ve got to enable one site of mine with this &#8220;<a href="http://developers.facebook.com/connect.php">Facebook Connect</a>&#8221; thingy, my primary goal being the increase of the conversion rate, in the way that people could use their Facebook account to log into my web site without a need for a boring registration process. Just click two buttons, and you&#8217;re in. If a person had a site account already, I&#8217;ll give him the opportunity to merge her Facebook account with the old one. Eventually, upon a succesfull &#8220;connect&#8221; round, a new account would be created within my application. It&#8217;s a cute <em>www</em>: a web win-win.</p>
<p>That is all what I needed, from Facebook: I do not need any fancy &#8220;get your friends here&#8221; or &#8220;post in your wall&#8221; rubbish. Simple and plain &#8220;single sign on&#8221;, among Facebook and my application.</p>
<p>After a quick search, the tutorials I found <a href="http://www.goldsteintech.com/facebook_connect/tutorial.php">here</a> and there were very <em>disappointing</em> (just a point of view, no offense), with a lot of useless PHP code. So I give you just one more tutorial that focuses more on the process than in the copy &amp; paste kind of help.</p>
<p>I will use PHP for some examples, and FB for &#8220;Facebook&#8221; so bear with me and give it a spin.</p>
<p>What you need:</p>
<ul>
<li>a FB account, if you don&#8217;t own one already. Unlikely; I&#8217;ll skip this step :)</li>
<li>a new FB application need to be created</li>
<li>the FB PHP library, to easily talk to their RESTful API</li>
<li>Some code refactoring of your own</li>
</ul>
<h3><strong>Creating a new FB application.</strong></h3>
<p>Two paths, here:</p>
<ul>
<li>the <a href="http://www.facebook.com/developers/createapp.php">long and detailed multistep form</a></li>
<li><a href="http://www.facebook.com/developers/createapp.php"></a>the <a href="http://developers.facebook.com/setup.php">super easy wizard</a></li>
</ul>
<p>Note that, whichever way you&#8217;ll choose, you will end up creating the exact same thing: an FB application (all your applications are stored <a href="http://www.facebook.com/developers/">here</a>).</p>
<p>We&#8217;ll use <strong>the wizard</strong>:</p>
<ul>
<li>the <strong>website name</strong> is just the name of the application (the handle by which you&#8217;ll refer to it in the application list).</li>
<li>the <strong>website url</strong> is self explanatory. Note that you could use ANY kind of URL, even something like &#8220;http://myapplication.localdomain&#8221;. In fact, FB will not need to contact the site, so you can use an development URL as well. For the test purposes I created two applications: one for the &#8220;real thing&#8221; and one for the mydomain.local devel environment.</li>
</ul>
<p>The next step will ask you to download a file. You can put the file WHEREVER you want. The root directory of your site is the first choice, and it&#8217;s where the wizard will try to find it. There&#8217;s no need for the wizard to find the file, though: it&#8217;s only a test. You can download the file and skip the test pressing &#8220;<em>Upload later</em>&#8220;. Two words about that particular file: the file is part of the system that permits our application to be proxied to FB (and vice-versa). I will not enter in details here, but think about this file as your &#8220;gateway&#8221; to and from the FB requests (<a href="http://wiki.developers.facebook.com/index.php/Cross_Domain_Communication_Channel">more info</a>). The other thing is: there is NOTHING in that file that identifies you application. Every FB application of the world could use the same xd_receiver.htm file. The file name itself is not important&#8230; it&#8217;s just the default. More on this later.</p>
<p>The third step is the landing page for your newly created FB application. Just a comfy place to pick things up. You need to write down the <strong>API KEY</strong> and the <strong>SECRET</strong> strings. You&#8217;ll use them from within your application.</p>
<p>What you also need to take from this page is the JavaScript snippet at point 1) and the <a href="http://wiki.developers.facebook.com/index.php/Category:FBML_tags">FBML</a> markup for the &#8220;Connect with Facebook&#8221; button at point 2).</p>
<p>Enough for the FB setup.</p>
<h3><strong>The FB PHP Library and the FB API</strong></h3>
<p>This is a set of helper PHP classes that will give you a simple PHP API interface to the <a href="http://wiki.developers.facebook.com/index.php/API">RESTful FB API</a>. You could even NOT use the library, and talk directly to the REST endpoints with some code of your own. Your choice.<br />
The FB API are <a href="http://wiki.developers.facebook.com/index.php/API">documented here</a>, while you can download the latest version of the PHP 5 Library via SVN</p>
<pre class="brush: jscript; light: true; wrap-lines: false;">
svn co http://svn.facebook.com/svnroot/platform/clients/php/trunk/ fb
</pre>
<p> (more info on <a href="http://wiki.developers.facebook.com/index.php/User:Client_Libraries">FB Client libraries</a>)</p>
<p>We are finally ready to get our hands dirty.</p>
<h3><strong>Hands on the code</strong></h3>
<p>We first need to focus on how things works, at an higher level. What will happen when a not logged-in user will click the &#8220;Connect with Facebook&#8221; button? Using some sort of <em>magic</em> Facebook will test if the user is currently logged in Facebook. If it&#8217;s not, it will spawn a <em>popup window</em> asking two things: the user has to agree to give your application access to her data, and after that Facebook will ask her for credential (username/password, as usual). Upon a succesful login, FB will redirect the user to your application toward an url of your choice (let&#8217;s call it the &#8220;<em>FB Connect endpoint</em>&#8220;).<br />
If the user is already logged in Facebook and has already given permission to our application to read some of her data, the login process will be completely transparent. The user clicks on our FB blue-ish button, and he will be instantly logged in our application too. Oh, joy.</p>
<p>As for the HTML part, you will place the FBML markup for the login button you got beforehand wherever you want. The FBML have to be programmatically converted in HTML by JavaScript, and for that to work you need to include the JavaScript snippet you already have. Put it at the end of your &lt;body&gt;, for example, and then run the FB.init() function. This function will be passed two parameters: the API KEY and the name and location of the &#8220;gateway&#8221; file. As said before, if the second parameter is not passed, the default is &#8220;xd_receiver.htm&#8221; on the document root.</p>
<pre class="brush: jscript; highlight: [2]; wrap-lines: false;">
  &lt;!-- The login button, somewhere in your page --&gt;
  &lt;fb:login-button onlogin=&quot;window.location='/fbconnect.php';&quot; v=&quot;2&quot; size=&quot;medium&quot;&gt;Login with Facebook&lt;/fb:login-button&gt;
  ...
  &lt;!-- At the page end of the page, presumibly --&gt;
  &lt;script src=&quot;http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php/en_US&quot;&gt;&lt;/script&gt;
  &lt;script&gt;
    FB.init(&quot;b4bbr43b4bb3b3b3&quot;);
  &lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>The interesting part, here, is in the highlighted row. The <strong>onlogin</strong> attribute contains what we pretentiously called the &#8220;FB Connect endpoint&#8221;. The onlogin attribute value is a JavaScript snippet that will be automatically run after a succesfull FB login. What we say here is: after a login, please redirect the user to our &#8220;fbconnect.php&#8221; page.<br />
The fbconnect.php page is where <em>OUR magic</em> will happen. There, we will use the FB Client API to ask FB some questions: &#8220;Is this guy a Facebook, logged in user?&#8221;, &#8220;Can you please give me the picture of him?&#8221;&#8230; and so on.<br />
The code, here, is given only as an example; think about it more as a <em>pseudocode,</em> really. The actual code I use is somewhat more complex, and I needed to dumb it down a little for clarity.</p>
<pre class="brush: php; wrap-lines: false;">
require(&quot;fb/facebook.php&quot;);
$fb = new Facebook(&quot;b4bbr43b4bb3b3b3&quot;, &quot;supersecret1234&quot;);
/* If it's present, ask FB for the ID of the current user */
$fbId = $fb-&gt;get_loggedin_user();
if (empty($fbId)) {
  /* No FB user, redirect to home page */
  header(&quot;Location: /&quot;);
}
/* Now we know the FB user id.
   It's our responsability to ask our Database if we already know him */
</pre>
<p>You need to have a way to find one user of yours given its FB user id. For instance I used a &#8220;fb_id&#8221; column in my Users table.</p>
<p>What happens now is up to you. There will presumably be two major cases:</p>
<ol>
<li>the user already exists: I programmatically login him into my application, and redirect to the home page as a logged in user.</li>
<li>the user does not exist: I give her the choice to merge her old account or to create a new one</li>
</ol>
<p>For the merge part, I need him to login with his credentials and then update the Users table with his FB id.</p>
<p>For the registration part, I luckily do not need anything else that FB can&#8217;t give me; so it&#8217;s a matter of pushing a button. The code is something like that:</p>
<pre class="brush: php; wrap-lines: false;">
$fbInfo = $fb-&gt;api_client-&gt;users_getInfo($fbId, array('name', 'pic_square', 'proxied_email'));
$model = new User();
$name = $fbInfo['name'];
$tries=1;
# find a unique username
do {
  $user = $model-&gt;fetchRow($model-&gt;select()-&gt;where('username=?', $name));
  if (!$user)
    break;
  else
    $name = $name . ($tries++);
} while(TRUE);
$user = $model-&gt;createRow();
$user-&gt;username = $name;
$user-&gt;avatar = $fbInfo['pic_square'];
$user-&gt;name = $fbInfo['name'];
$user-&gt;fb_proxy_email = $fbInfo['proxied_email'];
$user-&gt;fb_id = $fbId;
...
$user-&gt;save();
...
</pre>
<p>Finally, let me complain a bit: I find writing code that interacts with Facebook a very frustrating experience. The information you need is scattered all over the web (and the Facebook site itself, even), and examples quality is very poor. The technology behing FB is very sophisticated, though, and no matter what they try to do, you need to grasp some concepts that are sometimes quite difficult to master or a nightmare to debug.</p>
<p>Good luck.</p>
<h3>Further readings</h3>
<ul>
<li><a href="http://developers.facebook.com/connect.php">The official Facebook Connect page</a> (and the developers <a href="http://wiki.developers.facebook.com/index.php/Facebook_Connect">Wiki one</a>)</li>
<li><a href="http://wiki.developers.facebook.com/index.php/How_Connect_Authentication_Works">How Connect Authentication Works</a></li>
<li><a href="http://mashable.com/2009/01/12/facebook-connect-implementations/">10 Great Implementations of Facebook Connect</a></li>
<li>What is behind <a href="http://wiki.developers.facebook.com/index.php/Cross_Domain_Communication">Cross Domain Communication</a> and <a href="http://msdn.microsoft.com/en-us/library/bb735305.aspx">even more</a></li>
<li><a href="http://wiki.developers.facebook.com/index.php/API">Facebook API</a> documentation</li>
</ul>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://claudio.cicali.name/post/2010/01/use-facebook-connect-in-your-application/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://claudio.cicali.name/post/2010/01/use-facebook-connect-in-your-application/&amp;title=Use+Facebook+Connect+in+your+application" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://claudio.cicali.name/post/2010/01/use-facebook-connect-in-your-application/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://claudio.cicali.name/post/2010/01/use-facebook-connect-in-your-application/&amp;title=Use+Facebook+Connect+in+your+application" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Use+Facebook+Connect+in+your+application+-+&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://claudio.cicali.name/post/2010/01/use-facebook-connect-in-your-application/&amp;t=Use+Facebook+Connect+in+your+application" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-friendfeed">
			<a href="http://www.friendfeed.com/share?title=Use+Facebook+Connect+in+your+application&amp;link=http://claudio.cicali.name/post/2010/01/use-facebook-connect-in-your-application/" rel="nofollow" class="external" title="Share this on FriendFeed">Share this on FriendFeed</a>
		</li>
		<li class="shr-tumblr">
			<a href="http://www.tumblr.com/share?v=3&amp;u=http%3A%2F%2Fclaudio.cicali.name%2Fpost%2F2010%2F01%2Fuse-facebook-connect-in-your-application%2F&amp;t=Use+Facebook+Connect+in+your+application" rel="nofollow" class="external" title="Share this on Tumblr">Share this on Tumblr</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://claudio.cicali.name/post/2010/01/use-facebook-connect-in-your-application/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->