Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getLoginStatus always returns not_authorized

I am new to developing websites with Facebook SDK. So please bear with me.

Below is my simple code for verifying that I am logged into Facebook. For some reason I always get the 'not_autherized' response, even though I am the only developer of my app. The app number I provide is correct.

<!DOCTYPE html>
<html lang="en">
  <head>
  </head>
  <body>
    <div id="fb-root"></div>
    <script>
      window.fbAsyncInit = function() {
          // init the FB JS SDK
          FB.init({
              appId      : 'censored app id', // App ID from the app dashboard
              status     : true,              // Check Facebook Login status
              cookies    : true,
              xfbml      : true               // Look for social plugins on the page
          });

          FB.getLoginStatus(checkLoginStatus);
          function checkLoginStatus(response) {
              if (response && response == 'connected') {
                  alert('User is authorized!');
              } else {
                  alert('User not authorized!!!');
              }
          };

          // Additional initialization code such as adding Event Listeners goes here
      };

      // Load the SDK asynchronously
      (function(d, s, id) {
          if (d.getElementById(id)) return;

          var js = d.createElement(s); js.id = id;
          js.src = "//connect.facebook.net/en_US/all.js";

          var fjs = d.getElementsByTagName(s)[0];
          fjs.parentNode.insertBefore(js, fjs);
      }(document, 'script', 'facebook-jssdk'));
    </script>
  </body>
</html>

Is there anything I have overlooked?

Much appreciated :)

like image 848
Alon Granot Avatar asked Aug 03 '13 17:08

Alon Granot


1 Answers

A friend helped me with this. He very helpfully pointed out something that Facebook has neglected to document. When you create a new facebook application, then it is NOT automatically authorized by the admin and developers. An authorized application is one that exists in the "Account Settings->Apps" list. If it is not in this list, then it is not authorized.

This means that you need to call FB.login() at some point in your code in order to popup the user authorization window.

Mind you, this should be called from a button. Otherwise, the popup could be blocked.

Hope this helps someone else but me.

Cheers :)

like image 190
Alon Granot Avatar answered Nov 13 '22 18:11

Alon Granot