Archive for the ‘facebook’ Category

Facebook Java API

21/10/2009

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);
client.setIsDesktop(true);
String token = client.auth_createToken();
HttpClientParams params = new HttpClientParams();
HttpState initialState = new HttpState();
http.setParams(params);
http.setState(initialState);
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();
  post.releaseConnection();

  // 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…

Advertisements