我在使用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错误?
任何建议都将不胜感激!
发布于 2012-08-16 05:13:53
是的,只需将你的fbookinvite_check包装在begin/rescue中,在这里你从OAuthException中解救出来,然后为你的应用程序返回一些合理的东西。
你回答了你自己的问题:
也许有一种方法可以在不关闭应用程序的情况下捕获@graph.get_object错误?
将您的逻辑封装在begin/rescue块中,如下所示:
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
endhttps://stackoverflow.com/questions/11977233
复制相似问题