Many Social Networking sites allow users to authenticate through their APIs – this means that rather than needing to have a complete registration system on your site, you can simply allow users to authenticate with a third-party service and then grab their profile picture and name for use in your commenting systems or other scripts. In today’s tutorial, I’m going to show you how to add the most popular of these services (Facebook Connect) to your website in 3 steps.
Step 1 – Setup a brand new Facebook Application
To get started you’re going to need a Facebook Platform API key for your site. Follow these steps to create an application with the Facebook Developer application.
Step 2 – Create a Cross-Domain Communication Channel File
You’ll need to create a cross-domain communication channel file called “xd_receiver.htm” and save it in a directory relative to the callback URL.
For example, if you’re using http://www.site.com/ as your callback URL, but you want to store your Facebook Connect files in their own subdirectory, say http://www.site.com/connect. You should create the “xd_receiver.htm” file in the directory from where you’ll be serving your Connect Web pages (http://www.site.com/connect in our example).
Below is an example of a cross-domain channel file:
<!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" >
<body>
<script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/
XdCommReceiver.js" mce_src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/
XdCommReceiver.js" type="text/javascript"></script>
</body>
</html>
Important note: If you don’t have a cross-domain channel file present, Facebook won’t be able to communicate with your server correctly so make sure you don’t skip this step. You’ve also got the option of calling the file anything you’d like as long as you remember to change the filename mentioned in the FB.init method :)
Step 3 – The FB Connect JavaScript API Library
One of the easiest ways to begin using Facebook Connect on your site is through Facebook’s terrific JavaScript API. Include references to it. One of the essentials to connecting to the library and these are setting the correct document type as follows:
<!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">
and loading the Javascript library at the END of your webpage, near the </body> tag.
<script type="text/javascript" src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/
FeatureLoader.js.php" mce_src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/
FeatureLoader.js.php"> </script>
Always check the Facebook Developer page for the latest version of the Javascript Library.
Step 4 – A demo application using the JavaScript API Library
Here is a simple but effective example of how you can login a user to your Facebook Connect application and show their name and profile picture from Facebook.
src="http://blarnee.com/wp/wp-content/uploads/fbe_thumb.jpg" width="292" border="0" />
<!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>
<div id="user">
Name: <input name="name" size="27"><br />
<fb:login-button length=’long’ onlogin="update_user_box();"></fb:login-button>
</div>
<script type="text/javascript">
function update_user_box() {
var user_box = document.getElementById("user");
user_box.innerHTML =
"<span>"
+ "<fb:profile-pic uid=’loggedinuser’ facebook-logo=’true’></fb:profile-pic>"
+ "Welcome, <fb:name uid=’loggedinuser’ useyou=’false’></fb:name>"
+ "</span>";
FB.XFBML.Host.parseDomTree();
}
</script>
<script type="text/javascript" src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/
FeatureLoader.js.php" mce_src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/
FeatureLoader.js.php"> </script>
<script type="text/javascript">
var api_key = "API KEY GOES HERE";
var channel_path = "xd_receiver.htm";
FB.init(api_key, channel_path, {"ifUserConnected": update_user_box});
</script>
</body>
</html>
The FB tags you see here are XFBML (Extensible Facebook mark-up language) that you can learn more about here. You can easily extend this example to the comments or profile section of your website. To test this on your server, you’ll need to replace your API key in the code example.
You can download the Sources for this tutorial or view a demo.
and thats it!. I hope this tutorial has been helpful.
Share
Related posts:
Related posts brought to you by Yet Another Related Posts Plugin.
RSS feed for comments on this post. TrackBack URL
October 3rd, 2009 at 4:48 pm
[...] This post was mentioned on Twitter by Web Development News. Web Development News said: How to Add Facebook Connect To Your Website in just 3 Steps: Many Social Networking sites allow .. http://bit.ly/tOGKV [...]
November 6th, 2009 at 2:36 pm
Here's a tutorial that focuses on integrating Facebook Connect to an already existing dynamic site and covers topics like linking a user's Facebook Identity to their site identity.
Facebook Connect Tutorial: http://www.goldsteintech.com/facebook_connect/tut...
December 15th, 2009 at 8:46 am
i want to thank you for posting these helpful videos and story about how to connect blog with facebook.