I am building an application which uses Facebook to allow users to login. It is working fine on one page and I am managing to retrieve data from Facebook no problem at all.
However as soon as I click away from the home page onto say the profile page despite having the exact same code as on the index page no information appears, not even the Facebook login button. However when I go back to the index page the information is still there. As far as I am aware an access token is being generated.
The code I am using is below (I have removed the App ID):
// This is called with the results from from FB.getLoginStatus().
function statusChangeCallback(response) {
// The response object is returned with a status field that lets the
// app know the current login status of the person.
if (response.status === 'connected') {
// Logged into your app and Facebook.
getUserInfo();
getPhoto();
} else if (response.status === 'not_authorized') {
// The person is logged into Facebook, but not your app.
// document.getElementById('status').innerHTML = 'Please log ' + 'into this app.';
} else {
// The person is not logged into Facebook, so we're not sure if they are logged into this app or not.
// document.getElementById('status').innerHTML = 'Please log ' + 'into Facebook.';
}
}
// This function is called when someone finishes with the Login Button.
function checkLoginState() {
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
}
window.fbAsyncInit = function() {
FB.init({
appId : '',
cookie : true, // enable cookies to allow the server to access
// the session
xfbml : true, // parse social plugins on this page
version : 'v2.2' // use version 2.1
});
///Posts something to user facebook
FB.login(function(response){
}, {scope: 'publish_actions'});
// Once the JavaScript SDK is initialized, FB.getLoginStatus() is called. This function gets the state of the person visiting this page
//and can return one of three states to the callback you provide. They can be:
// 1. Logged into your app ('connected')
// 2. Logged into Facebook, but not your app ('not_authorized')
// 3. Not logged into Facebook and can't tell if they are logged into
// your app or not.
//
// These three cases are handled in the callback function.
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
if (response.status === 'connected') {
console.log(response.authResponse.accessToken);
}
});
};
// Load the SDK asynchronously
(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/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
//////////////////////////////Code to get public profile information from FACEBOOK
function getUserInfo() {
FB.api('/me', function(response) {
var str="<b>Name</b> : "+response.name+"<br>";
str +="<b>Link: </b>"+response.link+"<br>";
str +="<b>id: </b>"+response.id+"<br>";
str +="<b>Email:</b> "+response.email+"<br>";
document.getElementById("status").innerHTML=str;
});
}
function getPhoto()
{
FB.api('/me/picture?type=normal', function(response) {
var str="<br/><img src='"+response.data.url+"'/>";
document.getElementById("profilepicture").innerHTML+=str;
});
}
Any help would be appreciated.
Aucun commentaire:
Enregistrer un commentaire