Sunday, May 15, 2011

Facebook Login on Google App Engine(Java)

In this blog i will explain how you can use facebook login on your google app engine java application and how to publish on logged in user's facebook wall.

Frameworks/libraries i will be using are
1) Google App Engine Java
2) facebook-java-api(V 3.0.2)
3) Spring MVC(You can easily avoid it if you want to)

First thing i should warn you this facebook-java-api is not being supported actively so if you get into trouble you may find bit bit difficult to find a solution. But i hope using this tutorial you may not hit to any problem

Create Facebook Login Url
Login Url should be in the following Format<Your Facebook Api Key1>&connect_display=popup&v=1.0&next=<Callback Url after user login and authorize app2>&cancel_url=<Callback Url after user do not login or do not authorize app3>&fbconnect=true&return_session=true&session_key_only=true&req_perms=<permissions4>
1 Your facebook application API key. For more information visit Facebook Developer
2 Your site's url after login at facebook is succesfull and user authorize your application to use his/her data.
3 Your site's url after user declined to login at facebook or do not authorize your application to use his/her data.
4 Kind of information you want to access on behalf of User. Click here for more info on permission.

Once your url is ready, either directly display it on your html or hide it behind proxy url(using redirect on server side code).
Using Sprice MVC(annotaion based you can write login function as follows)

@RequestMapping(value = "/login", method = RequestMethod.GET)
 public String login(@RequestParam("op") String provider,@RequestParam("target") String target,HttpServletRequest request,Model model){
 String redirectUrl = null;
     String uri = request.getRequestURI();
     String requestUrl = request.getRequestURL().toString();
     int ind = requestUrl.lastIndexOf(uri);
     String serverName = requestUrl.substring(0, ind);
     //using provider param you can use many other logins like google etc but this example is only for facebook
     request.getSession().setAttribute("target", target);
     String returnUrl = serverName+"/logindone";
     return "redirect:"+redirectUrl;

Call back url implementation
Using Sprice MVC(annotaion based you can write function as follows)

@RequestMapping(value = "/logindone", method = RequestMethod.GET)
    public String logindone(HttpServletRequest request,Model model) throws AppException{
        String target = (String)request.getSession().getAttribute("target");// save your site url in session when you are going to Login url, so that you can send user back to site url where he clicked on Login url
                JSONObject facebookCode = new JSONObject(request.getParameter("session"));
                String sessionKey = facebookCode.getString("session_key");
                FacebookJsonRestClient jsonRestClient = new FacebookJsonRestClient("API_KEY", "SECRET",sessionKey);
                long userUid = jsonRestClient.users_getLoggedInUser();
                JSONArray userInfoArray = (JSONArray)jsonRestClient.fql_query("select first_name,last_name,username, email,sex from user where uid = me()");

                String emailId = (String)userInfoArray.getJSONObject(0).get("email");
                String firstName = (String)userInfoArray.getJSONObject(0).get("first_name");
                String lastName = (String)userInfoArray.getJSONObject(0).get("last_name");
                String sex = (String)userInfoArray.getJSONObject(0).get("sex");
 //Now you can publish this on Facebook if you want to. Just brag about your site on facebook :)
                        Attachment attachment = new Attachment();
                        attachment.setCaption(loggedInApplUser.getFirstName()+ " has joined to Do Something");
                        attachment.setDescription("My Site is fastest growing website in my frield in India.");
                        AttachmentMedia media = new AttachmentMediaImage("","");
                        jsonRestClient.stream_publish(loggedInApplUser.getFirstName()+ " has joined to do something", 
                                attachment, null, null, null);
                    }catch(Exception ex){
        return "redirect:"+target;


  1. Thank you. Found it interesting and useful. Java is a general purpose, high-level, class-based and object-oriented programming language. And we provide Java course in Chennai at Fita.

  2. Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care and we take your comments to heart.As always, we appreciate your confidence and trust in us
    Java Training in Chennai

  3. High technology or high tech refers to the kind of technology that is cutting edge, advanced, and currently available. High technology does not refer to a class of because all kinds of technology have been considered high tech at one point, before being considered low tech or obsolete as time passes.

  4. Information have a peek at these guys and internet technology have had a very wide growth in recent years. Insight technologies would help people perform multimedia functions, internet connections, social networking and many more. Many of such insight technologies give us the best out of the simple interface. The powers of such technologies have given rise to many technology companies overnight.

  5. keep up the good work. this is an Assam post. this to helpful, i have reading here all post. i am impressed. thank you. this is our digital marketing training center. This is an online certificate course
    digital marketing training in bangalore /

  6. This article presents the problem and challenge technology is creating for the human condition: Inhumanity. Technology is brilliant, but cold, lacking heart and soul; the fundamental essence of human qualities. We present one unique and effective process designed to bridge the gap between the needs of human development and ever progressing, high-speed technology: The Alliance. antminer u3 setup guide