I'm building a JSP application and I would like to use Facebook Connect as one path for user registration and authentication, but I'm not finding much information about how to fetch and parse the FB cookie or even the right flow. I'm trying to merge the information found in the official documentation with a step by step guide like this one but for Java. I am not opposed to relying on libraries like Social Java but understanding the steps would be helpful. Here are the 3 use cases I'm trying to satisfy.
- Unauthenticated/unregistered user on my site clicks on "Facebook Connect" button to sign up (capturing email, name and profile ID) and and sign in.
- Unauthenticated user clicks on ""Facebook Connect" button to create a valid session on my domain.
- Authenticated and registered user without a connected Facebook profile clicks on "Facebook Connect" and associates a Facebook profile ID (and the option to update their email and name) with their existing profile.
For this project I have a Profile class that looks like this (I'm using the excellent Project Lombok with Hibernate)
@Entity
@Data
public class Profile implements java.io.Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String username;
private String password;
private String displayName;
private String email;
private String zipCode;
private String mobileNumber;
private String facebookId;
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
private DateTime dateCreated;
private int status;
private int level;
}
Status and Level really should be enums, but I'm trying to keep the code tiny for this question.
Disclaimer:I've been reading a lot of blogs about how to setup Facebook Connect for user registration and authentication, but they are for the most part based on PHP and older versions of the Facebook API (even some SO questions point to the old wiki in their accepted answers). This seems like a perfect application of the SO community.
Here is servlet solution I use. With little tweaking you can meke it work in any JSP with simple username-password form. No javascript needed!!! As far as address and phone number go read this: http://developers.facebook.com/blog/post/447
FBAuthServlet
FBEnableServlet
Haven't used it myself, but there seems to be a (inofficial) Java API on Google Code: http://code.google.com/p/facebook-java-api/
It doesn't take long to do the integration yourself, it's just OAuth 2.0 followed by an http request for some user details (json formatted).
We have some code in github (that's quite tied to our social model) that checks the OAuth token and returns the user id (link at bottom of post). You can get the current user's access token from the cookie that the Facebook Connect JavaScript writes (the cookie name begins 'fbs_').
https://github.com/mbst/common-social/blob/master/src/main/java/com/metabroadcast/common/social/auth/facebook/FacebookAccessTokenChecker.java