用于javascript的Facebook登录API不起作用

Facebook login API for javascript not working

本文关键字:API 不起作用 登录 Facebook javascript 用于      更新时间:2023-09-26

很抱歉缩进不正确。这是Facebook登录API。我的javascript编译器中出现了这些错误。。。

无效的应用程序Id:必须是表示应用程序Id的数字或数字字符串。"all.js:53

"在调用FB.init()之前调用了FB.getLoginStats()。"all.js:53

内容安全策略:无法分析无效的源chrome-extension://lifbcibllhkdhoafpjfnlhfpfgnpldfl

内容安全策略:无法解析无法识别的源chrome-

extension://lifbcibllhkdhoafpjfnlhfpfgnpldfl

得到https://www.facebook.com/plugins/login_button.php[HTTP/1.1 200 OK 1072ms]"fb:login_button在45秒内无法调整大小"

我肯定有正确的URL和app_id,我的URL是http://myimprint.herokuapp.com/

请提供任何帮助或指导。。。

 <html>
<body>
<div id="fb-root"></div>
<script>
   window.fbAsyncInit = function() {
   FB.init({
   appId      : '{283742071785556}',
   channelUrl : 'http://myimprint.herokuapp.com/',
   status     : true, // check login status
   cookie     : true, // enable cookies to allow the server to access the session
   xfbml      : true  // parse XFBML
     });
  // Here we subscribe to the auth.authResponseChange JavaScript event. This event is         fired
 // for any authentication related change, such as login, logout or session refresh.   This means that
 // whenever someone who was previously logged out tries to log in again, the correct case below 
  // will be handled. 
      FB.Event.subscribe('auth.authResponseChange', function(response) {
   // Here we specify what we do with the response anytime this event occurs. 
      if (response.status === 'connected') {
     // The response object is returned with a status field that lets the app know the current
    // login status of the person. In this case, we're handling the situation where they 
    // have logged in to the app.
  testAPI();
} else if (response.status === 'not_authorized') {
  // In this case, the person is logged into Facebook, but not into the app, so we call
  // FB.login() to prompt them to do so. 
  // In real-life usage, you wouldn't want to immediately prompt someone to login 
  // like this, for two reasons:
  // (1) JavaScript created popup windows are blocked by most browsers unless they 
  // result from direct interaction from people using the app (such as a mouse click)
  // (2) it is a bad experience to be continually prompted to login upon page load.
  FB.login();
} else {
  // In this case, the person is not logged into Facebook, so we call the login() 
  // function to prompt them to do so. Note that at this stage there is no indication
  // of whether they are logged into the app. If they aren't then they'll see the Login
  // dialog right after they log in to Facebook. 
  // The same caveats as above apply to the FB.login() call here.
  FB.login();
          }
        });
      };
   // Load the SDK asynchronously
   (function(d){
     var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement('script'); js.id = id; js.async = true;
     js.src = "//connect.facebook.net/en_US/all.js";
     ref.parentNode.insertBefore(js, ref);
    }(document));
  // Here we run a very simple test of the Graph API after login is successful. 
  // This testAPI() function is only called in those cases. 
   function testAPI() {
   console.log('Welcome!  Fetching your information.... ');
   FB.api('/me', function(response) {
  console.log('Good to see you, ' + response.name + '.');
   });
  }
  </script>
 <fb:login-button show-faces="true" width="200" max-rows="1"></fb:login-button>
 </body>
 </html>

Invalid App Id: Must be a number or numeric string representing the application id.

更改此行:

appId      : '{283742071785556}',

至:

appId      : "283742071785556",   /* numeric string */

appId      : 283742071785556,  /* number */
appId      : '283742071785556',

必须解决

问题

作为字符串的应用程序ID肯定是主要问题,但还有一件非常重要的事情:您应该始终在鼠标事件侦听器中使用FB.login。这就是为什么:

  • 用户不希望在打开您的网站/应用程序时立即收到授权/登录请求。首先,向他们展示您的网站/应用程序,并展示"登录"按钮/链接
  • 如果您不在鼠标事件侦听器中打开登录窗口,则弹出式阻止程序可能会阻止它。登录弹出窗口总是作为浏览器弹出窗口出现在外部网站上

顺便说一句,如果您在JavaScriptSDK中使用fb.login,那么您实际上并不需要"fb:login按钮"。

window.fbAsyncInit = function() {  
    // for js api initialization
    FB.init({
    appId  : '{Your-16 digit App ID}',
    status : true, // check login status
    cookie : true, // enable cookies to allow the server to access the session
    xfbml  : true, // parse XFBML
    oauth  : true // enable OAuth 2.0
    });
};

要解决此问题,只需在Facebook的"应用程序"部分执行"创建新应用程序"即可。然后返回Facebook社交插件页面,选择一个插件,并为其重新生成代码。

你的Facebook插件代码之前有一个应用程序ID:

<div id="fb-root"></div>
<script>(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>

在您将应用程序分配给插件之后

<div id="fb-root"></div>
<script>(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=<your-16-digit-app-id>";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>

注意到突出显示的代码中的差异了吗?请在评论区告诉我这是否解决了你的问题。