title: Facebook Authentication with JSP
body: <p>I'm trying to complete Facebook Authentication within a simple JSP page following this example: <a href="http://www.sergiy.ca/how-to-implement-facebook-oauth-2.0-app-authorization-process-in-java/" rel="nofollow">http://www.sergiy.ca/how-to-implement-facebook-oauth-2.0-app-authorization-process-in-java/</a></p>
  
  <p>Unfortunately, I'm not very successfull at this point. Your help would be appreciated. As developer of the app, I somehow managed to accept the app and I can see it in my app list. But when I log in as another user, I'm unable to accept the app. The user is not prompted to give access right to the app although the redirect request seems to have been sent to FB. Any help would be much appreciated. My code:</p>
  
  <pre><code>&lt;%@ page import="java.util.*,org.apache.commons.codec.binary.*, java.net.*, org.json.simple.*" %&gt;
  &lt;html&gt;
  &lt;body&gt;
  &lt;%
  String fbSecretKey = "efqec6fdedd17a64055712dcc7d81f58";
  String fbAppId = "116041890091";
  String fbCanvasPage = "http://apps.facebook.com/stupidgame/";
  String fbCanvasUrl = "http://stupidgame.com:8090/stupidgame/";
  String accessToken;
     if(request.getParameter("signed_request") != null) {
      //it is important to enable url-safe mode for Base64 encoder 
      Base64 base64 = new Base64(true);
      //split request into signature and data
      String[] signedRequest = request.getParameter("signed_request").split("\\.", 2);
      //parse signature
      String sig = new String(base64.decode(signedRequest[0].getBytes("UTF-8")));
      //parse data and convert to json object
      JSONObject data = (JSONObject)JSONValue.parse(new String(base64.decode(signedRequest[1].getBytes("UTF-8"))));
      //check if user authorized the app
      if(data.get("user_id")==null || data.get("oauth_token")==null) {        
          //this is guest, create authorization url that will be passed to javascript
          //note that redirect_uri (page the user will be forwarded to after authorization) is set to fbCanvasUrl
          response.sendRedirect("https://www.facebook.com/dialog/oauth?client_id=" + fbAppId + 
                  "&amp;redirect_uri=" + fbCanvasUrl + "&amp;scope=publish_stream,offline_access,email");
          return;
      }
      accessToken=data.get("oauth_token")+"";
  }else{
       response.sendRedirect("https://www.facebook.com/dialog/oauth?client_id=" + fbAppId + 
               "&amp;redirect_uri=" + URLEncoder.encode(fbCanvasUrl, "UTF-8") + 
               "&amp;scope=publish_stream,offline_access,email");
      return;
  }
  System.out.println("All set with accessToken:"+accessToken);
   %&gt;
   &lt;/body&gt;
   &lt;/html&gt;
  </code></pre>