javascript - FB.INIT 即使状态为 :true js sdk 也不会强制登录

标签 javascript facebook facebook-graph-api

我的应用允许用户使用 Facebook 请求对话发送请求。

我按如下方式运行 init:

<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({
    appId: '193078857407882',
    status: true,
    cookie: true,
     xfbml: false,
       });
</script>

根据我在文档中的理解,'status: true' 应该检查用户是否登录到 facebook。

但是在我的应用程序中,当用户未登录时,它只会显示以下错误消息:

An error occurred with hbg. Please try again later.

如何强制应用程序检查用户是否登录,如果没有登录?

Rails 3.0.7、Ruby 1.9.2、Js SDK

最佳答案

根据 FB.getLoginStatus 的 Facebook 文档, 如果你想收到传递 status: trueFB.init 的响应, 那么你需要 subscribeauth.statusChange 事件:

While you can call FB.getLoginStatus any time (for example, when the user tries to take a social action), most social apps need to know the user's status as soon as possible after the page loads. In this case, rather than calling FB.getLoginStatus explicitly, its possible to check the user's status by setting the status: true parameter with you call FB.init.

To receive the response of this call, you must subscribe to the auth.statusChange event. The response object passed by this event is identical to that which would be returned by calling FB.getLoginStatus explicitly.

所以在你的代码中你会做类似的事情

FB.Event.subscribe('auth.authResponseChange', function(response) {
    alert('The status of the session is: ' + response.status);
});

除非您改为订阅 auth.statusChange。但是请注意,根据 FB.Event.subscribe文档:

auth.statusChange() does not have a 'status' field.

关于javascript - FB.INIT 即使状态为 :true js sdk 也不会强制登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7252484/

相关文章:

facebook - 无法使用 Facebook graph api 创建广告创意

facebook - 如果我已经被授予 "user_status"权限,为什么还需要 FB 的 "user_posts"权限?

javascript - 根据文本检查前面的复选框

ios - Facebook 登录无法快速工作

javascript - 用于谷歌浏览器扩展的 Facebook JavaScript API

ios - iPhone:如何使用Facebook Graph API保存 session

java - 如何处理 Java 字符串中的 & 符号并将 URL 作为查询参数值发送?

javascript - AngularJS 在更改时克隆选择框并将所有选择框值应用到同一模型数组

javascript - 以正确的方式导入/注入(inject) TypeScript+AngularJS 模块

javascript - React 中的 jQuery 克隆不会将点击监听器传递给克隆