Retreiving facebook authtoken

时间:2017-10-21 17:23:27

标签: javascript ajax facebook flask

我正在尝试创建一个简单的页面,使用FB进行身份验证并检索有关用户的一些信息。问题是我只能在用户已经授权并登录的情况下才能获得令牌。

如果不是,则会弹出登录窗口,在我授权之后(我事先删除了应用程序)我得不到回复/回复。所以只有not authorized被记录在控制台中,但没有任何反应。只有当我重新加载页面时,我才会在其他网址上获得令牌ajaxed。这是脚本中的问题还是正常的?

感谢您的帮助!

<!DOCTYPE html>
<head>
   <title>My title</title>
</head>
<body>
<!-- <script src="static/js/jquery-1.9.0.js"> </script>
<script src="sendJSON.js"></script> -->
<script type="text/javascript"src="https://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js"></script>
<script>
  window.fbAsyncInit = function() {
    FB.init({
      appId      : 'xxx',
      cookie     : true,
      xfbml      : true,
      version    : 'v2.10'
    });

    FB.AppEvents.logPageView();   
      attachLoginStatusHandler(); 
  };

  (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 = "https://connect.facebook.net/en_US/sdk.js";
     fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));



  function attachLoginStatusHandler() {
    FB.getLoginStatus(function(response) {

    if (response.status === 'connected') {
    console.log("Known user");
    console.log(response.authResponse.userID);
    console.log(response.authResponse.accessToken);

     $.ajax({
                type: "POST",
                dataType: 'json',
                data:JSON.stringify(response),
                async:true,
                // data: JSON.stringify({help:"please"}),
                url: "/receive",
                contentType:"application/json; charset=utf-8",});

  } else {
    // the user is logged in to Facebook, 
    // but has not authenticated your app

    console.log("Not authorized");
    FB.login(function(response) {

   if (response.authResponse) {
     var access_token =   FB.getAuthResponse()['accessToken'];
     console.log('Access Token = '+ access_token);
     FB.api('/me', function(response) {
     console.log('Good to see you, ' + response.name + '.');
     });
   } else {
     console.log('User cancelled login or did not fully authorize.');
   }
 }, {scope: 'public_profile,email,user_likes,user_friends'});

  };
    });
}


</script>
    <div class="fb-login-button" onClick="fblogin"  scope="public_profile,email,user_likes" data-max-rows="1" data-size="large" data-button-type="continue_with" data-show-faces="true" data-auto-logout-link="false" data-use-continue-as="false"></div>


</body>

0 个答案:

没有答案