Connect/Linking Your Users to Facebook
From Facebook Developer Wiki
Benefits
Finding your users' Facebook friends that are also registered through your site has many benefits.
Building a Very Social Experience for Your Users
Facebook Connect makes it very easy for you to automatically create a social graph for your users without reinventing the wheel on your own website. Before Facebook Connect, a website owner would spend great amounts of time helping users find new friends and linking them together. New architectures had to be built to handle the social graph (also a very tough problem to scale), and for every new site a user joined, they would be stuck re-creating the social graph that they had just spent time creating on other sites they belonged to.
That doesn't have to be the case any more. With Facebook Connect, you can automatically import a user's friends and relationships they have spent time building on Facebook, preventing them from needing to redo that process all over again on your site. Facebook has made this process simple. When a user logs into your site through Facebook, you can automatically retrieve that user's Facebook friends who also have accounts on your site. Then you can share those friends' activities and allow your users to interact with ease, increasing clicks and page views, and keeping users on your site much longer.
Now that you don't have to worry about building the Social Graph, you can now focus on the important matters of creating an engaging experience for your users and their friends.
Learn More about Your Users
You probably already have a large user base with a lot of registered accounts. By using the Friend Linking features, you can automatically match up a user's existing accounts with their Facebook accounts when they first connect to your site with Facebook Connect.
By integrating with Facebook Connect, you can utilize a user's existing Facebook account to identify them and their friends. You are able to identify not only that they are a Facebook user, but what their interests are, what they are saying on Facebook, where they are from, and more. The guess work of identifying real people has been removed with Facebook Connect, and you can seamlessly tie this back to your existing accounts and not risk a ton of duplication.
Spreading Facebook Connect Faster across Your User Base
What if you already have a login architecture that supports your existing user base? With Friend Linking, Facebook also provides you with the tools to help your users that have logged in through Facebook to automatically identify their friends on Facebook that already have accounts on your site. There is little effort necessary on your part!
How It Works
Building a Very Social Experience for Your Users
Using Facebook to build a social experience for your users is simple! Here are a few ways you can use it to automatically match up your users with their friends using Facebook:
Identifying Users' Facebook Friends
Using Facebook Connect to identify a user's friends is an automatic process, and requires no complicated importing or finding to connect. Here's how it works:
- Users log in to your website using their existing Facebook account and credentials.
- Automatically, your website content loads, and links your newly connected users to their friends who've already registered on your site. There's no need for a new user to search for these friends!

Identifying Users and Their Friends
Before you can link a user's friends, the user needs to link accounts between your site and Facebook. How you do this depends upon whether the user is a new or existing user on your site.
Logging In New Users
Users who have not yet created accounts on your system can do so using only their Facebook account. If they click the Connect with Facebook button on your site, you can automatically authenticate them and associate their Facebook account with a new account on your site. Then, as in the example above, you can automatically link them to friends from Facebook that already have accounts.
Inviting Your Existing Users to Log In Through Facebook
Using an advanced feature, you can identify which of your existing users are on Facebook, and send them a special Connect Request through Facebook to link their account. Doing so encourages your users to log in through Facebook so you can link their account and connect them with their friends. 
Linking Accounts
Once the user is logged in and connected, there are two ways they can link their Facebook account with your site if they have an existing account on your site:
- They log in to their account through the Connect with Facebook button.
- They log in to their account through your normal login process, but within their session they are presented with a "Connect with Facebook" button. Clicking this associates their account with Facebook and you can immediately and automatically connect them with their Facebook friends who also have accounts on your system.
Here's on real-life example of how this works:
Case Study: Digg.com
Digg.com, the popular tech news voting site, has integrated Facebook Connect in a manner that enables its users to automatically connect with other friends on Facebook and see what stories they're voting on within the site. Their integration is very straightforward, and works with both new users of Digg, as well as existing users.
For New Users
- Users click the Connect With Facebook button at the top of Digg.com to register using their Facebook credentials.
- Users are authenticated with Facebook, and provide some additional details about themselves, including a Digg username, a box is checked by default, asking whether they want to link to their friends or not.
- Leaving the defaults, every time a user's friends log into Digg.com through the Facebook Connect link for the first time, they are automatically added to the user's friends on Digg.com as well. There is a likely chance that many of the Digg user's existing friends on Facebook will already have done this so they'll immediately be linked to many more friends on Digg.com, with absolutely nothing to do on their part at registration. Also, when the user adds new friends on Facebook, if those friends are already Digg users they will also be added as friends on Digg.com, all automatically!
For Existing Users
Facebook Connect for existing users on Digg also works similar to the registration process above:
- A Digg user logs into Digg using their Digg credentials, and is presented with a Connect with Facebook button. Or they can just log in using their Facebook credentials through a similar Connect with Facebook button at log in.
- If this is their first time logging into Facebook through Digg, the user is presented with similar, but fewer options as above, again defaulting to have the user's friends on Facebook automatically become friends on Digg.com if they log into Digg through Facebook.
- As the user's Facebook friends log in through Facebook, they are automatically added as friends on Digg.com. There is a likely chance that many of the Digg user's existing friends on Facebook will already have done this so they'll immediately be linked to many more friends on Digg.com. Any new friends the user adds on Facebook that use Digg.com will also be added, automatically, as friends on Digg.com!
Linking Users to Their Accounts as They Register
Every time a user registers through your site without logging in through Facebook, you can still use the advanced method we mentioned above to invite those users to connect if they do have a Facebook account. Simply run the batch script we mentioned above to see if their email address matches one on Facebook, and they'll get a Connect invite if they're a match. Once they connect, Facebook will tell you which of your own accounts matches the Facebook account that just connected, and you can just match the two in your database and the two will forever be associated. There's no need for them to log in through your traditional login system or do anything special to have the user tell you which account is theirs. Facebook handles all that for you.
Enabling Users to Invite Friends Who already Have Accounts to Connect Accounts
We mentioned an advanced feature above that enables you to identify your users that are on Facebook, and automatically prompt them through Facebook to connect using their Facebook credentials. Now that you're using this special feature, how can you use that information to do something that prompts other users to join the site and invite their friends through Facebook? There are two ways you can do this:
- Assuming your users already have friends on Facebook that are using your site, but who have not yet connected, you can automatically provide a list of these friends for that user to invite.
- Once a user has invited all their friends to connect who are already using your site, you can prompt them to invite their other friends to log in through Facebook and create an account. This can be as simple as one click and they're registered!

Case Study: Huffington Post
Huffington Post, a news site that writes about politics, recently integrated Facebook Connect to share more news with their readers. When a reader is logged in through Facebook they have the opportunity to see all the articles their Facebook friends read on the site as well as any comments they make on the articles they read. Huffington Post made this easy by enabling users to quickly link their Facebook friends on the site. Here is how they did it:
- Any new or existing user has the opportunity to click a Connect with Facebook button on the site.
- Those users are given the opportunity to add additional preferences to their reading experience, such as posting to their Facebook Wall and other functionality. Note that most of these features are checked by default!
- Huffington Post automatically reads the user's friends on Facebook and pre-identifies those Huffington Post readers as Facebook users. Huffington Post is then able to pick out of your Facebook friends who is already a Huffington Post reader and add them as friends on Huffington Post.
- After users are logged in through Facebook, their Facebook friends that read Huffington Post are listed on the right in their list of friends on Huffington Post. Users now have the option to see what those friends read and what they comment on, finding new and interesting news in an intimate, close-knit environment.

Implementation
For information about how you link your users to Facebook, read How to Link Your Users to Facebook.
Best Practices
Keep these best practices in mind as you implement linking.
Keeping the Flow Natural
Users should never have to rebuild their social graph on your website. With Facebook Connect, you can very easily link a user's Facebook friends with no effort whatsoever by the user. If you want to enable options for the user, such as whether to also add them as friends on your website when they link accounts (as in the Digg.com example above), be sure to default those options to yes. You want the flow to be natural, automatic, and as simple as possible for the user to connect their Facebook account.
Customizing the User Experience
When the user logs into your site through Facebook, you'll want to immediately show data from their friends' activity on your site. A great example of this is the Videos application on Facebook. On this application, the minute you go to it, a list of the most recent videos shows up, along with videos tagged recently with your friends in them.
Unregistering Users After They Leave Your Site
When a user cancels their account on your website, you'll want to be sure to call connect.unregisterUsers so Facebook knows those Facebook users are no longer connected with your website. This will ensure your website no longer appears in their list of apps on Facebook.
Verifying Before Calling FB.Connect.inviteConnectUsers That Users Still Have Friends to Connect
Just to be sure, you'll want to call connect.getUnconnectedFriendsCount before loading a call to FB.Connect.inviteConnectUsers or fb:connect-form tag. This will ensure the user isn't pulling up an empty list of friends to invite.
Enabling Users to Only Invite Those They Haven't Invited Before
When you use the fb:request-form or fb:connect-form to generate invitations, you can track which friends that user invited. Facebook returns a list of IDs in an ids[] parameter returned to your URL specified in the action parameter of your fb:request-form tag. Simply read that parameter as an array and you can then store the list of IDs in a cookie or somewhere for safe keeping later. Then, the next time you call fb:request-form just include an exclude_ids attribute with the IDs that user has already selected (separated by commas), and only the users not previously selected will appear. This is generally good practice, and helps users remember who they have already invited.
Calling connect.registerUsers with a Very Large Email List
Facebook only allows for 1,000 email addresses at a time when running connect.registerUsers to invite users to connect on your system. Therefore, you'll need to organize your data in sets of 1,000 when processing, and only send them 1,000 at a time. So, for each connect.registerUsers call you'll want to send an array of 1,000 hashed e-mail addresses. You should send as many email addresses as possible (up to the 1,000) in a single call to Facebook.
