3

Tutorial: use Java to develop Facebook applications

 3 years ago
source link: https://marco.dev/2009/04/27/tutorial-use-java-to-develop-facebook-applications-example/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

java-version.com: What's new in Java 16? 15? Keep up to date!

Tutorial: use Java to develop Facebook applications

Create a FB application is funny and easy with Java. There are some open source libraries that can help you to achieve your goal. You find these libraires here :

https://wiki.developers.facebook.com/index.php/Java

I used the facebook-java-api to create this tutorial. It’s a good library but unfortunately it miss documentation (in the traditional OSS old style 😉 )

https://code.google.com/p/facebook-java-api/

I used SpringMVC as framework, it’s very easy to pass values to web pages with Spring.

I created 2 pages , one for the login (index.htm) and a second page (results.htm) that shows the results :

Code of the first page :

IFacebookRestClient fbClient = new FacebookJsonRestClient(API_KEY, SECRET_CODE)

FacebookWebappHelper fh = new FacebookWebappHelper(request, response, API_KEY, SECRET_CODE, fbClient);
        fh.requireLogin("");
        
        if (!fh.isLogin())
            return null;

The goal of this page is to login the user if is still not logged in. Is in your application configuration on Facebook that you can decide where to send the user after the succesful login.

The second page contains this code :

protected ModelAndView handleRequestInternal(
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
  String token = ServletRequestUtils.getStringParameter(request, "auth_token");
  IFacebookRestClient fbClient = new FacebookJsonRestClient(API_KEY, SECRET_CODE);
  fbClient.auth_getSession(token);

  // get your Facebook id
  Long id = fbClient.users_getLoggedInUser();
  
  // query Facebook database, ask for all friends single and give me their uid and their complete name
  JSONArray sqlResult = (JSONArray) fbClient.fql_query("SELECT uid, name FROM user WHERE  relationship_status='single' AND uid IN (SELECT uid2 FROM friend WHERE uid1 = " + id.toString() + ")");

  // write the result in a List
List users = new ArrayList();
        for (int i = 0; i < sqlResults.length(); i++) {
            User usr = new User();
            usr.setUid(Long.valueOf(sqlResults.getJSONObject(i).getString("uid")));
            usr.setName(sqlResults.getJSONObject(i).getString("name"));
            users.add(usr);
        }

// send the result to the webpage
ModelAndView mav = new ModelAndView();
mav.addObject("users", users);
return mav;
}
}
[/sourcecode]

This code fragment return the list of my friends single and I put this friends in an ArrayList of User (class included in facebook-java-api).
Show the results in a jsp page is very easy :

[sourcecode language='html']
<table border="1">
              <tr><td>First name</td><td>Name</td></tr>      
              <tr><td>${user.uid}</td> <td>${user.name}</td></tr>
</table>

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK