Status Post to facebook wall using FQL(Facebook Query Language) query by java

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

Download required 'restfb-1.6.11.jar',Get it from here.

Create index.jsp:
<body> <a href=" 550398948320179&redirect_uri=http://facebookstatus.appspot. com/PostStatusServlet" >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 PostStatusServlet is different to know how to give this see my preview post (see fig 14 and 15 ).

When you hit click here 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.

You can download Sourcecode from here

Create the

import; import; import; import; 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; 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"); HttpSession session = request.getSession(true); String MY_ACCESS_TOKEN = ""; String authURL = " access_token?client_id=550398948320179&redirect_uri= secret=e02b5619629ae3652071ca096519252e&code="+ code; URL url = new URL(authURL);"url: " + url); String result = readURL(url);"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 // setting attribute using session so that the attribute is available for entire application session.setAttribute("MY_ACCESS_TOKEN", MY_ACCESS_TOKEN); // forwarding all the details to PostStatus.jsp page getServletConfig().getServletContext() .getRequestDispatcher("/PostStatus.jsp") .forward(request, response); } private String readURL(URL url) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); InputStream is = url.openStream(); int r; while ((r = != -1) { baos.write(r); } return new String(baos.toByteArray()); } }
Create PostStatus.jsp:
<body> <form action="PostStatusCode.jsp"> Post Status to Facebook Wall : <input type="text" name="posttext"> <br> <input type="submit" name = "submit" value ="post"> </form> </body>
<body> // code for post the status to facebook wall
<% String postText = request.getParameter("posttext"); String MY_ACCESS_TOKEN = (String) session.getAttribute("MY_ACCESS_TOKEN"); FacebookClient facebookClient = new DefaultFacebookClient(MY_ACCESS_TOKEN); // fql query for post status FacebookType publishMessageResponse = facebookClient.publish("me/feed", FacebookType. class,Parameter.with("message", postText)); %>
<h1><%=publishMessageResponse.getId()%></h1> </body>
Output Screen:
Facebook Login page

click here to post on facebook wall

 post status on facebook wall

message successfully posted on facebook wall
HTML Comment Box is loading comments...