Ben
Ben

Reputation: 249

Facebook javascript events not fired any way i turn it

I am using Facebook JavaScript SDK with my site, there is an option there to set events when user login/logout from Facebook so i can show him notifications in my website. can't manage it to work, tried everything, also using offline_access permission.

here is my html code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:og="http://ogp.me/ns#" xmlns:fb="http://www.facebook.com/2008/fbml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta name="description" content="blabla" />
        <meta name="keywords" content="blabla" />
        <meta property="og:title" content="blabla" /> 
        <meta property="og:url" content="http://XXX/" />
        <meta property="fb:app_id" content="XXX" />
        <title>Facebook Notifier</title>
        <link href="<?php echo base_url(); ?>styles/style.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
        <script type="text/javascript" src="<?php echo base_url(); ?>js/jquery.cookie.js"></script>
        <script src="http://connect.facebook.net/en_US/all.js"></script>
        <script type="text/javascript" src="<?php echo base_url(); ?>js/global.js"></script>
    </head>
    <body>
<div id="fb-root"></div>
    <div id="my_div">
    </div>
</a>
</body></html>

here is my js code:

FB.init({
        appId  : 'XXX',
        status : true, // check login status
        cookie : true, // enable cookies to allow the server to access the session
        xfbml  : true, // parse XFBML
        oauth : true, // enables OAuth 2.0
        channelUrl : 'http://XXX/channelUrl'
    });

    FB.getLoginStatus(function(response) {
        if (response.authResponse) {
            connected = true;
        }
        else{
            connected = false;
        }
    }, true);

    FB.Event.subscribe('auth.authResponseChange', function(response) {
        if(response.authResponse) connected = true;
        else connected = false;
    });

in the end of this js code i have a function that run every few seconds checking the connection variable, its a global one. and it not changed i tried to login/logout from facebook and no event fires. only when i refresh the page i can get the user status. in firefox firebug console i get the message (also tried in chrome, ie): uncaught exception: Error: https://www.facebook.com Proxy.InstallTrigger i tried to solve it by doing everything i can find about this error in google and also in stackoverflow and still not working. btw: when i wrote XXX in the original files its my url or my appid. thanks.

Upvotes: 1

Views: 2624

Answers (5)

J. Minjire
J. Minjire

Reputation: 1088

If you're in 2022 wondering why it won't fire. Enable 3rd party cookies, this worked for me.

Upvotes: 0

Frank Bruemmer
Frank Bruemmer

Reputation: 1

I'm all new into FB development and ran into the same issue a couple days ago.

I tried pretty much all answers and solutions that are out there but nothing worked out. The damn event wouldn't want to fire no matter what I did in my latest VS project. However I remembered getting this to work in my first VS project where I followed a tutorial on the same matter. After a while I checked that initial project and to my surprise it fired there and not in my other one.

Finally I realized that the Canvas Url on FB (app settings) was the culprit : h t t p: // localhost:15497/

It was set to my localhost with the port used in my other project. I changed the port number and suddenly it also worked in my other project.

Make sure that the canvas url in the FB app settings matches where your Visual Studio runs the app. Also change the value once you deploy your web app.

Upvotes: 0

nav
nav

Reputation: 3115

I've always been a bit skeptical about the auth. events the Facebook fires so in your case I'd prob just setup a poller myself to check the auth status of the connected users. Like this:

var connected;

FB.init({
    appId  : 'XXX',
    status : true, // check login status
    cookie : true, // enable cookies to allow the server to access the session
    xfbml  : true, // parse XFBML
    oauth : true, // enables OAuth 2.0
    channelUrl : 'http://XXX/channelUrl'
});


FB.getLoginStatus(handleUserStateChange);
FB.Event.subscribe('auth.authResponseChange', handleUserStateChange);

function handleUserStateChange(response) {
    connected = !!response.authResponse;
}

setInterval(function() {
    FB.getLoginStatus(handleUserStateChange, true);
}, 10000)   // poll every 10 seconds

Also as you defined status: true in the FB.init config object you don't need to pass the second parameter to FB.getLoginStatus at the start as that forces a check to Facebook instead of reading from the stored variable (which is already the latest information as we just initd).

Upvotes: 2

Vlad
Vlad

Reputation: 1763

I am doing FB.Event.subscribe in a function that is invoked on document ready event:

<script>(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=...";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

$("document").ready(function(){
    FB.Event.subscribe('edge.create',
      function(response) {
        alert('You liked the URL: ' + response);
      });
});
</script>

Or here there is another example of making sure that FB is initialised before you do any invocations.

Upvotes: 0

Teddy
Teddy

Reputation: 18572

It looks like you might have a variable scope problem with connected.

Something like this might help:

var connected; // define/initialize connected variable outside functions
FB.init({
    appId  : 'XXX',
    status : true, // check login status
    cookie : true, // enable cookies to allow the server to access the session
    xfbml  : true, // parse XFBML
    oauth : true, // enables OAuth 2.0
    channelUrl : 'http://XXX/channelUrl'
});

FB.getLoginStatus(function(response) {
    if (response.authResponse) {
        connected = true;
    }
    else{
        connected = false;
    }
}, true);

FB.Event.subscribe('auth.authResponseChange', function(response) {
    if(response.authResponse) connected = true;
    else connected = false;
});

I would use console.log() from within your functions that respond to subscribed events to see what is really happening (or not happening). Then write your real code based on what console.log() tells you.

Upvotes: 0

Related Questions