0

I have googled and looked at other similar questions posted on Stackoverflow but none of them seem to work. I need to be able to login using the FB login on my website and then retrieve my friend list. I used the code from FB developer's site and I can login but nothing else executes. In particular I would like to understand two things:

  1. How can I modify this code to turn the login into a logout button once I am logged in?
  2. How can I get the testAPI() function to execute?

Here is my entire code: the login works but I see no output in my JS console.

<html>   
<title> Test Title</title>    
<body>
<h1> About Test Title </h1>     
<p>Random text.</p>
<div id="fb-root"></div>
<script>
   window.fbAsyncInit = function() {
   FB.init({
      appId      : '345435435435', // valid App ID is used here
      channelUrl : '//www.my website.com/channel.html', // Channel File
      status     : true, // check login status
      cookie     : true, // enable cookies to allow the server to access the session
      xfbml      : true  // parse XFBML
   });

   // Here we subscribe to the auth.authResponseChange JavaScript event. This event is fired
   // for any authentication related change, such as login, logout or session refresh.               This means that
   // whenever someone who was previously logged out tries to log in again, the correct case below 
   // will be handled. 

   FB.Event.subscribe('auth.authResponseChange', function(response) {

   // Here we specify what we do with the response anytime this event occurs. 
   if (response.status === 'connected') {
     // The response object is returned with a status field that lets the app know the current
     // login status of the person. In this case, we're handling the situation where they 
     // have logged in to the app.

      console.log('Connected case… ');
      testAPI();
   } else if (response.status === 'not_authorized') {
     // In this case, the person is logged into Facebook, but not into the app, so we call
     // FB.login() to prompt them to do so. 
     // In real-life usage, you wouldn't want to immediately prompt someone to login 
     // like this, for two reasons:
     // (1) JavaScript created popup windows are blocked by most browsers unless they 
     // result from direct interaction from people using the app (such as a mouse click)
     // (2) it is a bad experience to be continually prompted to login upon page load.

     FB.login();
     console.log('Not authorized case… ');

   } else {
    // In this case, the person is not logged into Facebook, so we call the login() 
    // function to prompt them to do so. Note that at this stage there is no indication
    // of whether they are logged into the app. If they aren't then they'll see the Login
    // dialog right after they log in to Facebook. 
    // The same caveats as above apply to the FB.login() call here.

    FB.login();
    console.log('else case… ');
    }
  });
};

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

// Here we run a very simple test of the Graph API after login is successful. 
// This testAPI() function is only called in those cases. 

function testAPI() {
console.log('Welcome!  Fetching your information.... ');
  FB.api('/me', function(response) {
  console.log('Good to see you, ' + response.name + '.');
 });
}
</script>
<!--
Below we include the Login Button social plugin. This button uses the JavaScript SDK to
present a graphical Login button that triggers the FB.login() function when clicked.

Learn more about options for the login button plugin:
/docs/reference/plugins/login/ -->

<fb:login-button autologoutlink="true" show-faces="true" width="200" max-rows="1"></fb:login-button>
 </body>
 </html>
Anvesh Saxena
  • 4,458
  • 2
  • 23
  • 30
user2399453
  • 2,930
  • 5
  • 33
  • 60
  • gets `testAPI` called? If yes, what is the output of `console.log(response)`? – Axel Amthor Jun 18 '13 at 04:38
  • I do not see any output in the Javascript console. Is this the right place to look for output? I also see the following output at the beginning in the JS console: Given URL is not allowed by the Application configuration.: One or more of the given URLs is not allowed by the App's settings. It must match the Website URL or Canvas URL, or the domain must be a subdomain of one of the App's domains. – user2399453 Jun 18 '13 at 05:27
  • Please check [this question](http://facebook.stackoverflow.com/questions/14474122/given-url-is-not-permitted-by-the-application-configuration/) as I think you are having a little issue in configuration of Application – Anvesh Saxena Jun 18 '13 at 05:37
  • Thanks, yes that thread is relevant. However when I tried the suggestion "You need to fill the value for Website with Facebook Login with the value http://localhost/OfferDrive/", I still get the same error. I waited for a few minutes after saving the setting. – user2399453 Jun 18 '13 at 06:11

2 Answers2

0

oFor the logout, you can use

FB.logout(function(response) {
    //console.log(response);
    // do something usefull ...
});

and put that on a button. BUT: you are currently logging off the user from Facebook AND from your app, users might find that strange?

Axel Amthor
  • 10,980
  • 1
  • 25
  • 44
  • I am new to this area so I am not sure I tried your suggestion the right way. How do I put this code on a button? I am trying to learn this for a side experimental project. Are there good links to get some background on this? I am only familiar with html and css and a tiny bit of JS. – user2399453 Jun 18 '13 at 05:23
  • For dealing with asynchronous FB requests, a 'tiny bit of JS' might be a big hurdle. Anyway, see next answer. – Axel Amthor Jun 18 '13 at 05:27
  • Yes, it might prove too much but I am keen to learn. I have many years of C programming experience however. – user2399453 Jun 18 '13 at 05:37
0

Logout on a button as requested:

<a href='#' onClick='doLogout();'>Logout from Facebook</a>

...

function doLogout() {
    FB.logout(function(response) {
       //console.log(response);
       alert("Bye bye...");
     });
}
Axel Amthor
  • 10,980
  • 1
  • 25
  • 44
  • When I do this I see the logout button but then clicking gives this error: FB.logout() called without an access token. all.js:55 i all.js:55 ca.auth.logout.transform all.js:95 da.prepareCall all.js:95 m all.js:96 i.provide.logout all.js:97 (anonymous function) all.js:80 (anonymous function) all.js:78 doLogout onclick – user2399453 Jun 18 '13 at 05:34
  • Any other thoughts? I learned a bit more JS on the side and I better understand JS code now. However I am not clear on what my app settings should be on FB. I have enabled "Website with Facebook login" and the URL I gave is my own website address (e.g www.xyz.com). I also have the same URL in the app domains field in Basic Info. What am I missing? I still continue to get "Given URL is not allowed by the Application configuration.: One or more of the given URLs is not allowed by the App's settings. It must match the Website URL or Canvas URL, or the domain must be a subdomain.. " – user2399453 Jun 21 '13 at 06:49