/**
   * Method that performs introspection on an AUTH string, and returns data as a String->String
   * hashmap.
   *
   * @param auth the authstring to query, as built by an auth impl.
   * @return the data from the introspect, in a map.
   * @throws IOException if anything goes wrong.
   */
  private Map<String, String> introspectAuth(String accesstoken) throws IOException {
    Map<String, String> results = new HashMap<String, String>();

    // create a fb client using the supplied access token
    FacebookClient client = new DefaultFacebookClient(accesstoken, Version.VERSION_2_5);

    try {
      // get back just the email, and name for the user, we'll get the id
      // for free.
      // fb only allows us to retrieve the things we asked for back in
      // FacebookAuth when creating the token.
      User userWithMetadata =
          client.fetchObject("me", User.class, Parameter.with("fields", "email,name"));

      results.put("valid", "true");
      results.put("email", userWithMetadata.getEmail());
      results.put("name", userWithMetadata.getName());
      results.put("id", "facebook:" + userWithMetadata.getId());

    } catch (FacebookOAuthException e) {
      results.clear();
      results.put("valid", "false");
    }

    return results;
  }
 /** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */
 @Override
 protected void doPost(HttpServletRequest request, HttpServletResponse response)
     throws ServletException, IOException {
   // TODO Auto-generated method stub
   PrintWriter out = response.getWriter();
   out.println("* Fetching single objects *");
   try {
     User user = fbClient.fetchObject("me", User.class);
     Page page = fbClient.fetchObject("tradesbyjack", Page.class);
     out.println("User name: " + user.getName());
     out.println("User name: " + user.getId());
     out.println("User name: " + user.getGender());
     out.println("Page likes: " + page.getLikes());
   } catch (FacebookOAuthException fbexp) {
     out.println("Cannot find user info. " + fbexp.getErrorMessage());
   }
 }
 public static void fbLogin() {
   String token = params.get("token");
   if (null != token && !token.isEmpty()) {
     FacebookClient fb = new DefaultFacebookClient(token);
     User fbUser = fb.fetchObject("me", User.class);
     Logger.info("Facebook User:"******"Such a user does not exists. Create/Register one...");
       // Register a new...
       // Email uniqueness is controlled by Facebook I suppose, so no need to check on our side...
       sesUser = new SUser(fbUser.getName(), fbUser.getEmail());
       sesUser.fbId = fbUser.getId();
       sesUser.save();
     }
     Auth.fbLogin(token, sesUser);
   } else {
     redirect("/");
   }
 }
  @Override
  public boolean evaluate(
      HttpServletRequest request, RuleInstance ruleInstance, AnonymousUser anonymousUser)
      throws Exception {

    JSONObject typeSettings = JSONFactoryUtil.createJSONObject(anonymousUser.getTypeSettings());

    User user = FacebookUtil.getFacebookUser(typeSettings.getString(WebKeys.FACEBOOK_ACCESS_TOKEN));

    String gender = ruleInstance.getTypeSettings();

    if (gender.equals("custom") && Validator.isNull(user.getGender())) {
      return true;
    }

    if (StringUtil.equalsIgnoreCase(gender, user.getGender())) {
      return true;
    }

    return false;
  }
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("<br><h4>we are getting data</h4>");
    String code = request.getParameter("code");
    out.println("<br>code: " + code);
    out.println("<br>");
    try {
      OAuthClientRequest requestOAuth =
          OAuthClientRequest.tokenLocation("https://graph.facebook.com/oauth/access_token")
              .setGrantType(GrantType.AUTHORIZATION_CODE)
              .setClientId(apiKey)
              .setClientSecret(secretKey)
              .setRedirectURI(redirectUri)
              .setCode(code)
              .buildBodyMessage();

      OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());

      GitHubTokenResponse oAuthResponse =
          oAuthClient.accessToken(requestOAuth, GitHubTokenResponse.class);
      accessToken = oAuthResponse.getAccessToken();
      expiresIn = oAuthResponse.getExpiresIn();
    } catch (OAuthSystemException ae) {
      ae.printStackTrace();
    } catch (OAuthProblemException pe) {
      pe.printStackTrace();
    }

    // out.println("<br>Access Token: " + accessToken);
    // out.println("<br>Expires In: " + expiresIn);

    try {
      FacebookClient facebookClient = new DefaultFacebookClient(accessToken);
      myFriends = facebookClient.fetchConnection("me/friends", User.class);
      myFeeds = facebookClient.fetchConnection("me/home", Post.class);

      for (User myFriend : myFriends.getData()) {
        f.add(myFriend.getName());
        out.println("<br>id: " + myFriend.getId() + " Name: " + myFriend.getName());
      }
      //	out.println("<br>");
      out.println("<br>f count: " + f.size());
    } catch (FacebookException e) {
      e.printStackTrace();
    }

    facebookDataBean fdb = new facebookDataBean();
    fdb.setName("zishan ali khan");
    HttpSession session = request.getSession();
    if (session != null) {
      session.setAttribute("myfdb", fdb);
      session.setAttribute("yourFriends", f);
      session.setAttribute("feeds", myFeeds);
      RequestDispatcher view = request.getRequestDispatcher("result.jsp");
      view.forward(request, response);
      f.clear();
      // out.println("<br>I am in");
    } else {
      // out.println("<br>Session Over");
    }
  }