【问题标题】:Facebook Koala Gem - Logging OutFacebook Koala Gem - 注销
【发布时间】:2012-08-15 21:02:17
【问题描述】:

我在使用 Koala Gem 注销时遇到问题,想知道它们是否相关。

以下是我的 Javascript 代码:

  <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>

下面是我的 Facebook 标签:

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

我的登出代码:

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

登录完美运行,我可以执行 api 调用没问题。但是,在我注销后,我收到以下错误:

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

以下是我的 fbookinvite_check 代码:

  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

问题似乎是cookie是访问令牌无效,重定向后@graph没有显示为nil。如果我刷新,则页面加载没有问题,但我在注销时收到错误。

也许有办法在不关闭应用程序的情况下捕获@graph.get_object 错误?

任何建议将不胜感激!!

【问题讨论】:

    标签: ruby-on-rails facebook facebook-graph-api koala


    【解决方案1】:

    是的,只需将您的 fbookinvite_check 包装在您从 OAuthException 救援的开始/救援中,然后为您的应用程序返回一些理智的东西。

    您回答了自己的问题:

    也许有一种方法可以在不捕获 @graph.get_object 错误的情况下 关闭应用程序?

    将您的逻辑包装在一个开始/救援块中,如下所示:

    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
    

    【讨论】:

    • 抱歉,我不确定我是否完全按照。您是否介意在代码中举个例子?谢谢你
    猜你喜欢
    • 2016-04-20
    • 1970-01-01
    • 1970-01-01
    • 2012-05-10
    • 1970-01-01
    • 2012-06-17
    • 2012-02-15
    • 2013-02-17
    • 1970-01-01
    相关资源
    最近更新 更多