繁体   English   中英

用于javascript的Facebook登录API无效

[英]Facebook login API for javascript not working

抱歉,这是一个糟糕的缩进。 这是Facebook登录API。 我在我的javascript编译器中收到这些错误...

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

“在调用FB.init()之前调用了FB.getLoginStatus()。” all.js:53

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

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

延伸:// lifbcibllhkdhoafpjfnlhfpfgnpldfl

获取https://www.facebook.com/plugins/login_button.php [HTTP / 1.1 200 OK 1072ms]“fb:login_button未能在45秒内调整大小”

我肯定有正确的网址和app_id,我的网址是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',

必须解决问题

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

  • 用户在打开您的网站/应用程序时不希望立即显示授权/登录请求。 首先,向他们展示您的网站/应用程序的内容,并显示“登录”按钮/链接。
  • 如果您不在鼠标事件监听器中打开登录窗口,弹出窗口阻止程序可能会阻止登录窗口。 登录弹出窗口始终显示为外部网站上的浏览器弹出窗口。

顺便说一句,如果你将FB.login与JavaScript SDK一起使用,你真的不需要“fb:login-button”按钮。

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 Social Plugins页面,选择一个插件,并为其重新生成代码。

在拥有应用ID之前您的Facebook插件代码:

<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>

请注意突出显示的代码中的区别? 如果这在注释区域解决了您的问题,请告诉我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM