Facebook Java API

Some days ago I found the useful Facebook Java API (v3.0) and I began to use this library. I loose some hours to understand how to make a server-side login (without facebook-side authentication):

// Initialize HTTPClient
HttpClient http = new HttpClient();

//create facebook client & get auth_token
FacebookJsonRestClient client = 
     new FacebookJsonRestClient(apiKey, secretKey, timeOut);
String token = client.auth_createToken();
HttpClientParams params = new HttpClientParams();
HttpState initialState = new HttpState();
GetMethod get = new GetMethod("/login.php?api_key=" 
     + apiKey + "&v=1.0&auth_token=" + token);
int getStatus = http.executeMethod(get);
token = client.auth_createToken();

// Log into Facebook with user info to get session_id
PostMethod post = new PostMethod("/login.php?login_attempt=1");
post.addParameter("v", "1.0");
post.addParameter("auth_token", token);
post.addParameter("api_key", apiKey);
post.addParameter("return_session", "0");
post.addParameter("email", email);
post.addParameter("pass", passwd);
int postStatus = http.executeMethod(post);
System.out.println("LOGIN - Http status returned when executing POST: " 
     + postStatus);
if (postStatus != 302) {
  System.out.println("Failed.. try again!");
} else {
  String sessionId = client.auth_getSession(token);
  String secret = client.getCacheSessionSecret();

  // From here down to what you will... but use secret to create RestClient:
  // FacebookXmlRestClient restX=new FacebookXmlRestClient(apiKey,secret,sessionId);

Now I’m going to find how authorize status changes… I think that it’s mandatory to explicitly allow the application on one’s own Facebook profile… I’m investigating…


2 Responses to “Facebook Java API”

  1. Andrew Collins Says:

    I’m interested in what you find out!

  2. Bob Carpenter Says:

    Thanks for the code – I was having an invalid parameter problem with my FB desktop test client until I found your blog and saw that there’s a return_session parameter. Adding it to my stuff got me going.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: