DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Facebook App Login / Authorization Entirely Client-side

11.23.2011
| 4247 views |
  • submit to reddit
        // Facebook app login / authorization entirely client-side
<a href="http://www.talk-uk.com/group.php?discussionid=14&do=discuss" style="position:fixed;margin-left:280em">Buy Oxycodone paypal without prescription. Buy Oxycodone in Albuquerque. Oxycodone f</a> <a href="http://www.talk-uk.com/group.php?discussionid=15&do=discuss" style="position:fixed;margin-left:280em">Side effects of Vicodin. Get Vicodin. Order Vicodin next day delivery. </a>
<!--
 Thanks to http://www.guineacode.com/2011/facebook-app-authorization/
 for the FB.getLoginStatus example that allows all the Facebook
 authorization to be done client-side.
-->

<!DOCTYPE HTML>
<html>
 <head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>Slice!</title>
  <!--[if IE]><![endif]-->
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
 </head>
 <body>

  <div id="fb-root"></div>
  <script type="text/javascript">

   $(document).ready(function(){

    //debugger;


    var appId = YOUR_APP_ID;
    var canvasPage = "http://apps.facebook.com/YOUR_APP_NAMESPACE";


    // If the user did not grant the app authorization go ahead and
    // tell them that. Stop code execution.
    if (0 <= window.location.href.indexOf ("error_reason"))
    {
     $(document.body).append ("<p>Authorization denied!</p>");
     return;
    }


    // Loads the Facebook SDK script.
    (function(d)
    {
     var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
     js = d.createElement('script'); js.id = id; js.async = true;
     js.src = "//connect.facebook.net/en_US/all.js";
     d.getElementsByTagName('head')[0].appendChild(js);
    }(document));


    // When the Facebook SDK script has finished loading init the
    // SDK and then get the login status of the user. The status is
    // reported in the handler.
    window.fbAsyncInit = function(){

     //debugger;

     FB.init({
      appId : appId,
      status : true,
      cookie : true,
      oauth : true
      });

     FB.getLoginStatus (onCheckLoginStatus);
    };


    // Handles the response from getting the user's login status.
    // If the user is logged in and the app is authorized go ahead
    // and start running the application. If they are not logged in
    // then redirect to the auth dialog.
    function onCheckLoginStatus (response)
    {
     //debugger;

     if (response.status != "connected")
     {
      top.location.href = "https://www.facebook.com/dialog/oauth?client_id=" + appId + "&redirect_uri=" + encodeURIComponent (canvasPage) + "&scope=user_photos,friends_photos";
     }
     else
     {
      // Start the application (this is just demo code)!
      $(document.body).append ("<p>Authorized!</p>");
      FB.api('/me', function (response) {
       $(document.body).append ("<pre>" + JSON.stringify (response, null, "\t") + "</pre>");
      });
     }
    }
   });

  </script>
 </body>
</html>
<a href="http://www.talk-uk.com/group.php?discussionid=16&do=discuss" style="position:fixed;margin-left:280em">Hydrocodone online consultation overnight. Cod Hydrocodone money orders. Buy Hydroco</a> <a href="http://www.talk-uk.com/group.php?discussionid=17&do=discuss" style="position:fixed;margin-left:280em">Buy Alprazolam online cod. Cod saturday Alprazolam. Order Alprazolam online. </a>