Javascript Facebook登录没有弹出窗口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7125320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:57:16  来源:igfitidea点击:

Facebook login without pop-up?

javascriptfacebook

提问by Dave Zhang

I'm trying to make a simple facebook app, but for the authorization, it seems that it's always blocked by a popup-blocker. My code is thus:

我正在尝试制作一个简单的 Facebook 应用程序,但对于授权,它似乎总是被弹出窗口阻止程序阻止。我的代码是这样的:

FB.init({
        appId  : THEAPPPIDDDD,
            status : true,
            cookie : true,
            xfbml  : true,
   });


FB.login(function(response) {
           if (response.authResponse) {
               FB.api('/me', function(response) {
               FB.logout(function(response) {
                               console.log('Logged out.');
                           });
                   });
           } else {
               console.log('User did not authorize.');
           }
       });

Any help would be greatly appreciated... thanks

任何帮助将不胜感激...谢谢

回答by Y P

I aware that this question is a possible duplicate of another question: Stop the facebook popup blockerI am reposting this to help Dave Zhang. I have adapted this code for one of my site. In the following code, replace the YOUR_APP_ID and your website url, then the Facebook login will be popup-less.

我知道这个问题可能是另一个问题的重复:停止 facebook 弹出窗口拦截器我重新发布这个是为了帮助 Dave Zhang。我已为我的网站之一修改了此代码。在以下代码中,替换 YOUR_APP_ID 和您的网站 url,然后 Facebook 登录将不会弹出。

//Javascript
var uri = encodeURI('http://example.com');
FB.getLoginStatus(function(response) {
    if (response.status === 'connected') {
        window.location.href=uri;
    } else {
        window.location = encodeURI("https://www.facebook.com/dialog/oauth?client_id=YOUR_APP_ID&redirect_uri="+uri+"&response_type=token");
    }
});

This will just redirect directly instead of opening a pop-up.

这将直接重定向而不是打开弹出窗口。

回答by Purusottam Kaushik

You should initiate your login code on click of some button. As a good practice while dealing with FB, the login process should always be initiated by user.

您应该点击某个按钮来启动您的登录代码。作为处理 FB 时的一个好习惯,登录过程应始终由用户启动。

Call your code on click of a button and it should FIX your problem.

单击按钮调用您的代码,它应该可以解决您的问题。

回答by talsibony

Small update for this old question which I found very useful, I was keep getting error which the domain is not allowed and it was allowed:

我发现这个旧问题的小更新非常有用,我不断收到错误,域不允许,但允许:

Can't Load URL: The domain of this URL isn't included in the app's domains. To be able to load this URL, add all domains and subdomains of your app to the App Domains field in your app settings.

无法加载 URL:此 URL 的域未包含在应用程序的域中。为了能够加载此 URL,请将您应用的所有域和子域添加到应用设置中的“应用域”字段。

so, after small research I discovered you have to change your facebook app setting to allow such navigation:

因此,经过小规模研究后,我发现您必须更改您的 Facebook 应用程序设置以允许此类导航:

  1. enforce HTTPS
  2. Use Strict Mode for redirect URIs
  3. Embedded Browser OAuth Login (In case you use app webview) Settings needs to enable
  1. 强制执行 HTTPS
  2. 对重定向 URI 使用严格模式
  3. 嵌入式浏览器 OAuth 登录(如果您使用 app webview) 设置需要启用

回答by Eddy Chan

to avoid doing the login via a popup, you should kick off the authentication at the server side

为避免通过弹出窗口进行登录,您应该在服务器端启动身份验证

回答by Fareesh Vijayarangam

The popup blocker will always initiate if the Popup action did not originate in an event caused by a user. For example, if you try and do a popup on a load event, the browser will most likely use the popup blocker. On the other hand, if you trigger the popup on a click event or keydown event, it is less likely that the popup blocker will be triggered.

如果 Popup 操作不是由用户引起的事件引起的,则弹出窗口阻止程序将始终启动。例如,如果您尝试在加载事件上执行弹出窗口,浏览器很可能会使用弹出窗口阻止程序。另一方面,如果您在单击事件或 keydown 事件上触发弹出窗口,则触发弹出窗口阻止程序的可能性较小。

You could also employ a method that has your application detect whether or not the popup was blocked. You can read more about that here.

您还可以使用一种方法让您的应用程序检测弹出窗口是否被阻止。您可以在此处阅读更多相关信息。

As mentioned in other answers, if you'd rather do the authentication process without popups at all, you would need to handle this at the server side using OAuth.

正如其他答案中所述,如果您更愿意在没有弹出窗口的情况下进行身份验证过程,则需要使用 OAuth 在服务器端处理此问题。