如何在页面加载时触发FB.login()?

 阳吉登 发布于 2023-02-13 18:48
  • php
  • 1 个回答
    • 所以我放弃了将登录代码放在单独的.js文件中的想法.我只是将FB.login方法window.fbAsyncInitFB.init代码一起放在块内的html中.它完美地运作.

         <body>
              <div id="fb-root"></div>
          <script>
      
        window.fbAsyncInit = function() {
      
            FB.init({
              appId      : '140*****07', // App ID
              channelUrl : '//http://s******.com/channel.html', // Channel File
              status     : true, // check login status
              cookie     : true, // enable cookies to allow the server to access the session
              oauth      : true,
              xfbml      : true  // parse XFBML
            });
      
          FB.login(function (response) {
      
                  if (response.status === "connected") {
      
                              var uID = response.authResponse.userID;
      
                              console.log(uID);
      
                            FB.api('/me', function (response) {
      
                                   var name = response.name;
                                   var locationName = ' ';
      
                                   if (response.location) {
      
                                      locationName = response.location.name;
                                      console.log(locationName);
      
                                  } else {
      
                                      alert("your current city needs to be set in your Facebook 'about' section. Please make it public for our use");
      
                                  }
                             });//closes fb.api
      
                          } else if (response.status === "not_authorized") {
      
                              //authCancelled. redirect
                          }
                      },
                      {
                          scope: 'user_location,user_likes'
                      }
                  );
      
      
            };//closes window.fbAsynInit
      
          // 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));
      
           </script> 
      
             <h1 id="redirecting">Redirecting</h1>
             <button id="button">click</button>
      
             <img id='backgroundImg' src="https://sa*****.com/template.jpg"/>
      
         </body>
      </html>
      

      2023-02-13 18:49 回答
    撰写答案
    今天,你开发时遇到什么问题呢?
    立即提问
    热门标签
    PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
    Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有