Is it possible to force the Facebook session to refresh, or alternatively, call the ‘FB.init’ function more than once in a document. I wont go into too much detail with what I am trying to achieve as it is quite long winded, but I have been working on this for a while and it definitely appears to be necessary to do this.
I have a script that is called every 30 seconds that checks if the facebook user is logged in, this is simply achieved with this:
window.fbAsyncInit=function(){
// Initialize the Facebook object
FB.init({
appId:"123",
status:true,
cookie:true,
xfbml:true,
oauth:true
});
// Check the Facebook session status every 30 seconds
setTimeout(function(){
// Pass the Facebook object to the welcome function
fb_welcome(FB);
,30000);
};
In the ‘fbWelcome’ function, I test to see if the user is logged in via Facebook using the getLoginStatus function. Dependant on the response, I then alter elements on the page. However, there is one key thing that I do that is making all of this go wrong.
If the user is logged into Facebook, then I send a request to my API using ajax, that returns the Facebook Users details. This API uses PHP to communicate with the Facebook API. If the user was logged into Facebook when the page loaded, there will be no problems. However, if the user was not logged into Facebook, and logs into Facebook on another tab whilst on my site, the session is not refreshed and the details cannot be accessed because the access token and signed request are not valid. I therefore need a way to refresh the access token and signed request in the session using javascript, to make sure they are read correctly by the PHP API.
I am pretty sure that by calling the FB.init function again, this will solve it as it will refresh the session. However, I am not sure if I am meant to call this function more than once per page. Is this permitted? Will it mess things about at all?
Just a few notes:
- I am aware that I could retrieve the users details via the Javascript SDK and FB.api, however, I do not want to do this for particular reasons
- I am also aware that I can pass the access token and signed request to the PHP API, but I also do not want to do this for particular reasons
Any help much appreciated
Although I have not answered my question, I have solved it using one of the solutions I did not want to use. I am now retrieving the users details using the Javascript API instead of my own API as I cannot get the new Facebook session to pass to the PHP API.
I tried to call the init function again, and although I was able to, it had absolutely no effect, plus I didn’t really want that to be the solution anyway as I shouldnt have to reinitialize the Facebook library just to refresh the session.
Anyway, I will leave this open to allow others to give their views, if they have any. It seems that trying to communicate between the PHP and Javascript SDK is a real challenge in some circumstances, the amount of problems I have had with it…