尝试在 React 和 Webpack 中使用 Facebook SDK 获取资源时出现网络错误

发布于 2025-01-19 09:53:21 字数 1465 浏览 0 评论 0原文

我正在尝试将Facebook SDK用于JavaScript,我希望用户登录,以便我可以获得FB返回的令牌,该过程成功完成,但我会遇到错误“尝试获取资源时的NetworkError”。在控制台。

这是我调用Facebook SDK并与我的应用程序连接的代码:

export function initFacebookSdk() {
  return new Promise(resolve => {
    // wait for facebook sdk to initialize before starting the react app
    window.fbAsyncInit = function () {
      window.FB.init({
        appId: <appId>,
        cookie: true,
        xfbml: true,
        version: 'v13.0'
      });
      resolve()
    };   
  });
}

export function loadFacebookSDK(d, s, id){
  return new Promise(resolve => {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) { return; }
    js = d.createElement(s); js.id = id;
    js.src = "https://connect.facebook.net/en_US/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
    resolve()
  })
}

这是我称之为使用Facebook登录的函数:

export const FbLogin = () => {
  return new Promise( resolve => {
    loadFacebookSDK(document, "script", "facebook-jssdk")
    initFacebookSdk()
    window.FB.login(response => {
      if (response.authResponse) {
        resolve(response);
      } else {
        resolve(response);
      }
    }, {scope: 'email'});
  })
}

代码的这一部分是调用函数并正确获得令牌的部分:

  const getFbToken = async (e) => {
    e.preventDefault()
    const data = await FbLogin();
    console.log(data);
  }

我需要帮助才能知道我做错了什么以及如何防止出现错误,目前我正在本地执行测试。

I am trying to use the Facebook SDK for javascript, I want the user to log in so I can get the token that FB returns, the process completes successfully but I get the error "NetworkError when attempting to fetch resource." in the console.

This is my code to call the facebook sdk and connect with my app:

export function initFacebookSdk() {
  return new Promise(resolve => {
    // wait for facebook sdk to initialize before starting the react app
    window.fbAsyncInit = function () {
      window.FB.init({
        appId: <appId>,
        cookie: true,
        xfbml: true,
        version: 'v13.0'
      });
      resolve()
    };   
  });
}

export function loadFacebookSDK(d, s, id){
  return new Promise(resolve => {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) { return; }
    js = d.createElement(s); js.id = id;
    js.src = "https://connect.facebook.net/en_US/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
    resolve()
  })
}

And this is the function I call to login with Facebook:

export const FbLogin = () => {
  return new Promise( resolve => {
    loadFacebookSDK(document, "script", "facebook-jssdk")
    initFacebookSdk()
    window.FB.login(response => {
      if (response.authResponse) {
        resolve(response);
      } else {
        resolve(response);
      }
    }, {scope: 'email'});
  })
}

This part of the code is the one that calls the function and obtains the token correctly:

  const getFbToken = async (e) => {
    e.preventDefault()
    const data = await FbLogin();
    console.log(data);
  }

I need help to know what I am doing wrong and how to prevent the error from appearing, for now I am performing the tests locally.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看轻我的陪伴 2025-01-26 09:53:21

NetworkError 表示请求无法通过。最常见的原因是:

  • 请求已被用户阻止,例如广告拦截器(Facebook 常见)
  • 端点的 CORS 策略不允许请求
  • 端点类型错误或协议 (https)丢失
  • 端点离线(对于 Facebook SDK 来说不太可能)
  • 客户端离线,或者(如果在服务器上运行)防火墙规则阻止访问

在 OP 的情况下,他们的广告拦截器阻止了对 Facebook 的请求。这很常见,因为相同的 SDK 用于收集用户数据和显示 Facebook 广告。

为了防止使用广告拦截器的用户获得不良的用户体验,您可能希望在请求被阻止时显示有意义的错误消息。 这篇文章中描述了一个巧妙的解决方案。

NetworkError means that the request was unable to go through. The most common causes of this are:

  • The request has been blocked by the user, such as an ad-blocker (common for Facebook)
  • The request is not allowed by the endpoint's CORS policy
  • The endpoint is miss-typed, or protocol (https) is missing
  • The endpoint is offline (unlikely for Facebook SDK)
  • The client is offline, or (if running on a server) firewall rules are preventing access

In the case of OP, their ad-blocker was blocking the request to Facebook. This is common, as the same SDK is used for collecting user data and displaying Facebook ads.

To prevent a poor user experience for users with an ad-blocker, you'd likely want to show a meaningful error message if the request was blocked. A neat solution to this is described in this post.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文