How to get facebook ‘user status’ using FQL (Facebook Query Language) query

Create facebook app and appengine application. To know more details how to create apps.

Create index.jsp :
		
<body> <a href="https://www.facebook.com/dialog/oauth?client_id= 550398948320179&redirect_uri=http://facebookstatus. appspot.com/StatusServlet" >Click Here</a> </body>

After creating facebook app you will get app_id and secret_key you need to replace the client_id in above link with your app_id, and the redirect_uri you will get after creating appengine application. In redirect_uri the term StatusServlet is different to know how to give this see my preview post (see fig 14 and 15 ).

When you hit Click Here link (index.jsp), it goes to facebook login page after success login it returns the access_token to our “StatusServlet” class with the use of redirect_uri shown above link.

Create the StatusServlet.java :

		
import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.List; import java.util.Locale; import java.util.logging.Logger; import javax.mail.Session; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.restfb.DefaultFacebookClient; import com.restfb.FacebookClient; import com.restfb.exception.FacebookException; import com.restfb.types.StatusMessage; public class FriendsListServlet extends HttpServlet { private static final long serialVersionUID = 1L; Logger l = Logger.getLogger("test"); @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String code = request.getParameter("code"); String localAddr = request.getLocalAddr(); Locale locale = request.getLocale(); String localName = request.getLocalName(); HttpSession session = request.getSession(true); l.info("code: " + code); l.info("local address " + localAddr); l.info("local " + locale); l.info("local name " + localName); System.out.println("local add " + localAddr); String MY_ACCESS_TOKEN = ""; String authURL = "https://graph.facebook.com/oauth/ access_token?client_id=550398948320179&redirect_uri= http://appoffacebook.appspot.com/StatusServlet& client_secret=e02b5619629ae3652071ca096519252e& code="+ code; URL url = new URL(authURL); l.info("url: " + url); String result = read_URL(url); l.info("result: " + result); String[] pairs = result.split("&"); for (String pair : pairs) { String[] kv = pair.split("="); if (kv[0].equals("access_token")) { MY_ACCESS_TOKEN = kv[1]; } } // end of for loop FacebookClient facebookClient = new DefaultFacebookClient( MY_ACCESS_TOKEN); List statusMessage = null; try { statusMessage = facebookClient.executeQuery("SELECT status_id, message FROM status WHERE uid=me()",StatusMessage.class); } catch (FacebookException e) { l.severe(e.getMessage()); e.printStackTrace(); } // setting attribute using session so that the attribute is available for entire application session.setAttribute("statusMessage", statusMessage); getServletConfig().getServletContext() .getRequestDispatcher("/StatusList.jsp") .forward(request, response); } private String read_URL(URL url) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); InputStream is = url.openStream(); int r; while ((r = is.read()) != -1) { baos.write(r); } return new String(baos.toByteArray()); } }

Create StatusList.jsp

		
<body> <a href="jsp/UserStatus.jsp" target="body"> click here to view your status</a> </body>

UserStatus.jsp :

		
<body> <% List<StatusMessage> statusMessage = (List<StatusMessage>) session.getAttribute("statusMessage"); %> <h1>Friends Status</h1> <table> <% for(StatusMessage status: statusMessage) { %> <td><%=status.getMessage()%></td> <% } %> </table> </body>

Output screen :

facebook status output

You need Source Code

HTML Comment Box is loading comments...