Facebook Koala Gem - Logging Out

I'm having issues logging out using the Koala Gem, and wondering whether they're related.

Below is my Javascript code:

  <script>
    window.fbAsyncInit = function() {
      FB.init({
        appId      : '310521258992725', // App ID
        channelUrl : '//localhost:3000/channel', // Channel File
        status     : true, // check login status
        cookie     : true, // enable cookies to allow the server to access the session
        xfbml      : true  // parse XFBML
      });
      // Additional initialization code here
      // whenever the user logs in, we refresh the page

      FB.Event.subscribe('auth.login', function() {
        setTimeout('document.location.reload()',0);
      });

      FB.logout(function(response) {
        FB.Auth.setAuthResponse(null, 'unknown');
        setTimeout('document.location.reload()',0);
      });
    };


    // 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));
  </script>

Below is my Facebook tag:

 <div id="fb-root"></div>

My logout code:

<a href="/" onclick="FB.logout();">Logout</a>

The login works perfectly and I can execute api calls no problem. However, after I logout, I get the following error:

OAuthException: Error validating access token: The session is invalid because the user logged out.  app/controllers/application_controller.rb:58: in 'fbookinvite_check'

Below is my fbookinvite_check code:

  def fbookinvite_check
    unless @facebook_cookies.nil?
      @access_token = @facebook_cookies["access_token"]
      @graph = Koala::Facebook::GraphAPI.new(@access_token)
      if !@graph.nil? == true
        @friends = @graph.get_object("/me/friends")
      end
    end
  end

The problem seems to be that the cookie is that the access token is invalidated, @graph is not showing as nil after the redirect. If I refresh, then the page loads no problem, but I get the error when I log out.

Perhaps there's a way to catch the @graph.get_object error without shutting down the application?

Any suggestions would be greatly appreciated!!

Answers


Yes, just wrap your fbookinvite_check in a begin/rescue where you rescue from OAuthException and then return something sane for your application.

You answered your own question:

Perhaps there's a way to catch the @graph.get_object error without shutting down the application?

Wrap your logic in a begin/rescue block like so:

def fbookinvite_check
  begin
    unless @facebook_cookies.nil?
      @access_token = @facebook_cookies["access_token"]
      @graph = Koala::Facebook::GraphAPI.new(@access_token)
      if !@graph.nil? == true
        @friends = @graph.get_object("/me/friends")
      end
    end
  rescue OAuthException => ex
    # handle the exception if you need to, or just ignore it if thats ok too
  end
end

Need Your Help

getCameraIdList() function returns an empty array

java android android-camera

I'm trying to work with Android's camera API 2, and I had a bug which I wasn't able to fix.

Print two-dimensional array in spiral order

arrays algorithm language-agnostic math

How do I print a 5×5 two-dimensional array in spiral order?