Social Java Social Application development using Java



SocialJavaAPIntroApplication: Description:
This example instantiates a Facebook client and uses Facebook Query Language (FQL) to get the Facebook User Name. This code can be used as the basis for using other Facebook API calls.

Handle Logged on User
Get the sessionKey and check if it is null. See full source code.
sessionKey = req.getParameter(FacebookParam.SESSION_KEY.toString()); // get sessionKey as request paramater

The format of the login page is: http://www.facebook.com/login.php?api_key=YOUR_API_KEY&v=1.0
In this example,
String loginPage = "http://www.facebook.com/login.php?api_key="+"00b0049fc7cf0d1e4a4ba2ea3e55b269"+"&v=1.0&canvas=true";
The canvas paramater is used to indicate that the page should be displayed as a Facebook Canvas page.

See Facebook Authentication Documentation. for more info on session key and login page format

Instantiate Facebook Client
A session key, api key, and api secret instantiate a Facebook client.
facebook = new FacebookJsonRestClient(appapikey, appsecret,sessionKey); // create Facebook Json Rest Client

The api_key was obtained by creating the Facebook application (see hello world example)
The session key is the same used to check whether the user is logged in.

Do Something with the Facebook Client API
Grab the user info that is a request parameter
user = req.getParameter("fb_sig_user");

Create a query to get the user name using Facebook Query Language (FQL)s


Prerequisites:
The Java Code :
Download Source
public class SocialJavaAPIIntroApp extends HttpServlet {

	//Application API Key and application secret from creating app in FB
	String appapikey = new String("00b0049fc7cf0d1e4a4ba2ea3e55b269"); //api key obtained after creating Facebook app
	String appsecret = new String("8ad8...api secret here..."); // api secret obtained after creating Facebook app

	//Facebook loginPage to this application.  Parameter canvas=true shows the result in Facebook canvas
	String loginPage = "http://www.facebook.com/login.php?api_key="+"00b0049fc7cf0d1e4a4ba2ea3e55b269"+"&v=1.0&canvas=true";
	FacebookJsonRestClient facebook;  // the facebook client, talks to REST Server
	PrintWriter servletOutput;  // output of servlet. HTML or FBML out

	public void doGet( HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException{
		servletOutput = res.getWriter(); // response is sent to ServletOutput
		res.setContentType( "text/html" );
		servletOutput.println("This is the doGet method");
	}

	public void doPost(HttpServletRequest req, 
			HttpServletResponse res) 
	throws ServletException, IOException {

		servletOutput = res.getWriter(); // response is sent to ServletOutput
		res.setContentType( "text/html" );
		servletOutput.println("This is the doPost method");
		servletOutput.println("<br>");

		String user =null;
		String sessionKey=null;
		sessionKey = req.getParameter(FacebookParam.SESSION_KEY.toString());  // Session Key passed as request parameter
		if (sessionKey==null) { // If there is not session key, they user not logged in
			servletOutput.println("");  // Facebook Redirect to login page
		}
		else{
			user = req.getParameter("fb_sig_user");  // get user as a string.  User info passed as request parameter
			servletOutput.println("User is " + user);  // displays numeric value
			servletOutput.println("<br>");
			facebook = new FacebookJsonRestClient(appapikey, appsecret,sessionKey);  // create Facebook Json Rest Client
			servletOutput.println("Facebook Client created");
			servletOutput.println("<br>");
			// with Facebook client created, now Facebook API calls can be made
			// In this case, a call to FQL to get username
			try{
				String query = "SELECT name FROM user WHERE uid=" + user;
				org.json.JSONArray resultArray = (org.json.JSONArray)facebook.fql_query(query); // query return an object.  Casting it as a String
				servletOutput.println("User Name is " + resultArray); 
			}
			catch( FacebookException ex )
			{
				servletOutput.println(">Error: Couldn't talk to Facebook> "  + ex );
			}
		} //else user is logged in
		servletOutput.close();
	}  // end doPost()
}


This is what is seen after adding the application in Facebook:

SocialJavaAPIIntroApp Screenshot