I'm trying to make a simple page with FB authenticating and retreive some info about a user. The trouble is I only can get to the token in case the user is already authorized and logged in.
In case they are not, the login window pops up and after I authorize it (I remove the app beforehand) I get no response/reply. So only not authorized
gets logged in the console but then nothing happens. Only when I reload the page I get the token ajaxed
on my other url. Is it problem in the script or is it normal?
Thanks for any help!
<!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>