首页 > 解决方案 > Facebook Javascript SDK - Fb.login 在浏览器内置的 Facebook 上不起作用

问题描述

我注意到 Facebook FB.Login 功能或 Facebook 登录按钮不再适用于在 Android 上使用 Facebook 的应用内浏览器打开的页面。(当用户单击 Facebook 应用程序内的 URL 时,它会在内置浏览器的应用程序上打开)。在 Messenger 上它工作正常。

不知道这个bug是从什么时候开始的。我甚至使用 FB.login 功能以及 Facebook 登录按钮(https://developers.facebook.com/docs/facebook-login/web/login-button)进行了测试。得到了他们两个相同的结果。它只是刷新页面。

示例代码:

<script>

function LoggedFb(){
       //This is just a sample code. This code when executed from a URL opened from Facebook's Browser ( When user clicks on a url on the Facebook App ), won't run. From Chrome or other browser works fine.
    alert("got in here");
}

</script>

<div class="fb-login-button" data-width="" data-size="large" data-button-type="continue_with" data-auto-logout-link="false" data-onlogin="LoggedFb" data-use-continue-as="false" data-scope=""></div>

</html>

上面的示例,例如在 Chrome 上运行时,将正常工作并调用 LoggedFb 函数。当使用此代码的页面从 Facebook 内置浏览器运行时,它只会刷新。

我也尝试过实现 FB.Login 功能,但它也不起作用。

我什至填写了错误报告(https://developers.facebook.com/support/bugs/456215991739392/),但他们说这不是错误。

从这开始,我看到很多网站的登录按钮都损坏了。其他人遇到这种情况,或者有解决方案吗?

谢谢。

标签: facebookfacebook-javascript-sdk

解决方案


每当访问来自 Facebook 移动浏览器时。单击 FB.login 不会执行任何操作,因为它已经假定您已登录。您需要做的是使用当前登录帐户的 accessToken 和 userID 来获取您需要使用 FB 的 API 的详细信息。

这是 Javascript 的代码示例:

signInWithFB(): void {
FB.getLoginStatus((response: any) => {
  if (response.status !== 'connected') {
    return FB.login((response: any) => {
      this.handleResponse(response)
    }, {
      scope: 'public_profile,email',
      enable_profile_selector: true,
      auth_type: 'rerequest',
      return_scopes: true
    });
  } else {
    this.handleResponse(response)
  }
});
}

handleResponse(response) {
FB.api(`/me?fields=email,picture,first_name,last_name`,
  (response: any) => {
    this._loginService.socialLogin({
      email: response.email ? response.email : '',
      profilePicture: {
        'url': response.picture.data.url,
        'key': ''
      },
      fullName: `${response.firstName} ${response.lastName}`,
      socialId: response.id,
      ...FACEBOOK_LOGIN
    }).
      subscribe(res => {
      })
  })
}

推荐阅读