Facebook javascript SDK FB.login在Facebook iFrame中不起作用

Facebook javascript SDK FB.login在Facebook iFrame中不起作用,javascript,facebook,facebook-javascript-sdk,facebook-authentication,Javascript,Facebook,Facebook Javascript Sdk,Facebook Authentication,我们正在使用Facebook JavaScript SDK来验证我们的Facebook应用程序。应用程序配置为画布URL为。下面是我们用来验证Facebook用户身份的代码 <script type="text/javascript"> window.fbAsyncInit = function() { debugger; FB.init({ app

我们正在使用Facebook JavaScript SDK来验证我们的Facebook应用程序。应用程序配置为画布URL为。下面是我们用来验证Facebook用户身份的代码

    <script type="text/javascript">
          window.fbAsyncInit = function() {
              debugger;
                  FB.init({
                      appId: '<%= ViewData["AppId"].ToString() %>', // App ID
                      channelUrl: '<%= ViewData["ChannelUrl"].ToString() %>', // Channel File
                      status: true, // check login status          
                      cookie: true, // enable cookies to allow the server to access the session
                      xfbml: true,  // parse XFBML
                      oauth: true
                  });

              FB.Event.subscribe('auth.login', function(response) {
                  if (response.status === 'connected') {
                      // the user is logged in and connected to your
                      // app, and response.authResponse supplies
                      // the user’s ID, a valid access token, a signed
                      // request, and the time the access token 
                      // and signed request each expire
                      var uid = response.authResponse.userID;
                      var accessToken = response.authResponse.accessToken;
                      startApp(uid, accessToken);
                  }

              });

          };

          // Load the SDK Asynchronously
          (function(d) {    
              var js, id = 'facebook-jssdk'; if (d.getElementById(id)) { return; }
              js = d.createElement('script'); js.id = id; js.async = true;
              js.src = "//connect.facebook.net/en_US/all.js";
              d.getElementsByTagName('head')[0].appendChild(js);
          } (document));




          function startApp(uid, accessToken) {                 
              var baseUrl = '<%= ViewData["AppBaseURL"].ToString() %>';
              var redirectUrl = baseUrl + '?uid=' + uid + '&accessToken=' + accessToken;
              window.top.location.href = redirectUrl;
              //document.location = redirectUrl;
          }          
       </script>
<fb:login-button onlogin="initiateLogin()" perms="email,user_about_me,user_birthday,friends_about_me,friends_birthday">Login with Facebook</fb:login-button>

window.fbAsyninit=函数(){
调试器;
FB.init({
appId:“”,//应用ID
channelUrl:“”,//通道文件
状态:true,//检查登录状态
cookie:true,//启用cookie以允许服务器访问会话
xfbml:true,//解析xfbml
真的吗
});
FB.Event.subscribe('auth.login',函数(响应){
如果(response.status===“已连接”){
//用户已登录并连接到您的
//app和response.authResponse提供
//用户ID、有效访问令牌、签名
//请求,以及访问令牌的时间
//和签名的请求都将过期
var uid=response.authResponse.userID;
var accessToken=response.authResponse.accessToken;
startApp(uid,accessToken);
}
});
};
//异步加载SDK
(职能(d){
var js,id='facebook jssdk';if(d.getElementById(id)){return;}
js=d.createElement('script');js.id=id;js.async=true;
js.src=“//connect.facebook.net/en_US/all.js”;
d、 getElementsByTagName('head')[0].appendChild(js);
}(文件);
函数startApp(uid,accessToken){
var baseUrl='';
var redirectUrl=baseUrl+'?uid='+uid+'&accessToken='+accessToken;
window.top.location.href=重定向URL;
//document.location=重定向URL;
}          
使用Facebook登录

当作为用户访问时,上述代码在Facebook中不起作用。但是,我们可以通过

访问它。问题与浏览器阻止弹出窗口有关。(事实上,在Facebook之外为你工作可能是因为你以前有时被允许这样做,对我来说,它被屏蔽了,也被应用程序屏蔽了)

在没有用户交互的情况下(如element
单击
或form
提交
),您不应调用
FB。登录
,以确保浏览器不会阻止登录窗口

更新:
你在这里混合了很多东西:

  • 第页<代码>http://facebook.elgifto.com/Home/Index
  • 正在使用(此应用程序没有
    命名空间
    !)
  • OAuth对话框的URL在
    客户id
    周围有
    (撇号),这是错误的
  • OAuth对话框不工作(可能也是由于应用程序设置、应用程序域?)
  • 应用程序位于
    http://apps.facebook.com/giftguru
    是且具有错误的画布URL证明

  • 我希望这对你有帮助

      <body>
          <div id="fb-root"></div>
          <script>
            window.fbAsyncInit = function() {
              FB.init({
                appId      : 'xxxxxxx',
                status     : true, 
                cookie     : true,
                xfbml      : true,
                oauth      : true,
              });
            };
            (function(d){
               var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
               js = d.createElement('script'); js.id = id; js.async = true;
               js.src = "//connect.facebook.net/en_US/all.js";
               d.getElementsByTagName('head')[0].appendChild(js);
             }(document));
    
           //LOGIN FUNCTION 
           function login() {
            FB.login(function(response) {
                if (response.authResponse) {
                    alert('Success!');
                }else{
                    alert('Login Failed!');
                }
            }, {scope: 'email'});
         }
          </script>
          <div onclick="login();">Login with Facebook</div>
        </body>
    
    
    window.fbAsyninit=函数(){
    FB.init({
    appId:'xxxxxxx',
    状态:正确,
    曲奇:是的,
    xfbml:是的,
    真的,
    });
    };
    (职能(d){
    var js,id='facebook jssdk';if(d.getElementById(id)){return;}
    js=d.createElement('script');js.id=id;js.async=true;
    js.src=“//connect.facebook.net/en_US/all.js”;
    d、 getElementsByTagName('head')[0].appendChild(js);
    }(文件);
    //登录功能
    函数登录(){
    FB.登录(功能(响应){
    if(response.authResponse){
    警惕(“成功!”);
    }否则{
    警报('登录失败!');
    }
    },{范围:'电子邮件'});
    }
    使用Facebook登录
    
    同时检查devopers.facebook.com上的应用程序是否未处于沙箱模式。我有一个问题,登录我的主帐户(应用程序管理员)工作正常,但与其他facebook帐户不工作。后来我意识到fb应用程序仍然处于沙盒模式,我必须添加每个作为测试人员或开发人员进行测试的帐户,或者禁用沙盒模式


    也许这会对某人有所帮助,我花了一个小时才弄明白

    如今,Facebook突然从使用Facebook对话框方法显示身份验证窗口转变为老式的弹出窗口。到目前为止,弹出窗口仅在Facebook选项卡/画布外运行应用程序时使用。这意味着您不能再在加载时运行
    FB.login
    ,而是使用单击事件来避免弹出窗口阻止程序


    我们在Facebook上找到的唯一文档是,但显然,这种弹出窗口方法已经在游戏中使用了好几个星期了。

    你说不工作是什么意思?!有错误吗?或者根本就没有加载框架?您是否设置了安全链接(SSL)?!我收到错误“连接已超时”。我尝试了此解决方案,但无效。我现在更改了代码,请查看更改的代码。当使用访问时,它可以工作。但是当我用访问它时,我得到“连接超时”。@mohang,您的画布未设置为
    http://facebook.elgifto.com/Home/Index/
    但是
    http://98.234.246.146:9999/html/home.html
    因此我无法看到它,并且check@mohang,仅此配置问题可能就是“连接超时”的原因错误…你能解释一下你说的是哪个配置问题吗?@mohang,我说的是你的画布URL没有指向你说的同一个页面(
    http://facebook.elgifto.com/Home/Index
    )但是