尝试在 React 和 Webpack 中使用 Facebook SDK 获取资源时出现网络错误
我正在尝试将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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NetworkError
表示请求无法通过。最常见的原因是:在 OP 的情况下,他们的广告拦截器阻止了对 Facebook 的请求。这很常见,因为相同的 SDK 用于收集用户数据和显示 Facebook 广告。
为了防止使用广告拦截器的用户获得不良的用户体验,您可能希望在请求被阻止时显示有意义的错误消息。 这篇文章中描述了一个巧妙的解决方案。
NetworkError
means that the request was unable to go through. The most common causes of this are: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.