I have been working on a Facebook application from last year. It was working good before the Facebook December 2012 changes. But now I'm getting an issue with a Facebook session.
I have replaced the old SDK with the new SDK, where it is written:
Avoid trying to reuse spent authorization codes
So when an authorization code is spent, the user session expires from my site. (I get authorization error after that).
The user gets logout from my application every 5-6 minute, so I have to redirect the user to loginurl again and page refresh and this is not good for my site, because I'm using an Ajax call on all links.
I have seen some applications which work good after December changes also. (for example, Wrapp.com). Wrapp.com doesn't redirect the user after authentication code expiry. And I can make a post on Facebook without redirect using Wrapp.com after 10 minutes of login too.
How does Wrapp.com handle authentication? What is the solution?
You have two ways:
You can extend the user access token using the scenario 4 tip described in Removal of offline_access permission.
In my case, I set my application as Desktop, so I have a token that expire in 90 days:
Avoid expiring a session with Ajax and Iframe pages using the PHP SDK.
After much research and testing, using PHP SDK "3.2.2" with in an iframe or an Ajax page that is loaded with canvas or page tab, will clear the session for the application if the called page is loaded more than one time.
EXAMPLE:
Your page tab loads an iframe inside its self containing PHP SDK, and your user interacts with the iframe. That iframe upon the second page load will clear the current application session.
SOLUTION:
Remove PHP SDK from Ajax and iframe pages in your application and use cURL instead.
If your iframe page inside your application requires user authentication information, it is suggested to created a cookie or session upon the user landing on the page tab/ canvas or upload the user authentication. Use this switch content where the user is required to view or interact.
Related
I read all the articles on FB regarding PHP login, JS login, Canvas app login, but something remains unclear to me. How does one keep a user logged in after the initial login in JavaScript? You receive a signed_request (which you can use to authenticate the user ONCE (on initial load of a canvas app and on login, right?), let's say you use that information to verify the user server-side. Ok, so far.. But what about subsequent page loads?
Basically my question is this.. Can I use the PHP FB SDK to verify a user is authenticated on each page load without an API call? How? Is there a way to do it that is compatible with canvas?
The reason I don't want any API calls is because I hit the limit once and my app went down for an entire night. Don't want that again...
A workaround I'm thinking of is to store the user token (which I want to anyway) and use that in a session/cookie to authenticate the user on every page load in PHP, but I'm not sure if this is the best approach, because: Using my own session/cookie would allow a user to stay logged in even if they are not logged in FB. Also, I presume FB wouldn't allow this for a canvas app.
I found these relevant questions, but the information is from 2011: PHP: Using Facebook OAuth with less API calls
Facebook Login: How to combine JavaScript with PHP SDK?
The workaround you mentioned is the right way of doing it. Saving tokens, using and updating them as they become expired - the way most companies stick to.
Only in cases of new users or expired tokens the OAuth should be used. The API you use is not important. You can actually retreive authentication through JavaScript and pass it to PHP for further usage.
Cheers.
I'm working on a facebook app using php sdk. I have one small problem with my app, after the user has logged into my app, if the user logs out of facebook from a new browser tab,after which any action on my app should be redirect back to login page saying user has logged out. but in my case my app is still working, even though he was logged out .so can anyone help me on this.
Note: I'm not using any logout URL in my app page. I can directly destroy the session by calling $facebook->destroySession(), but I don't know how do I conditional do it,since I can't know when the user logs out of facebook(opening in new browser tab)
If I logout from Facebook, while my app is running in an iframe, in another tab, I get a modal dialog "Login to Continue" served from Facebook. (using chrome)
If the same App is running outside Facebook (no iframe) in another tab, and I logout from Facebook, my App detects that the Facebook session is ended.
That's because I check on every page load if there is a valid Facebook user ;-)
My guess is that you don't check on every page load if the user is logged in.....
(or you have a funny browser that does not share cookies between tabs or something)
I have the exact same problem. I could not found a way to detect if the user is logged in using the PHP SDK. However, when I combine the JS SDK, it can detect the session and set's the cookie correctly. Then I pick the cookie/session using the PHP SDK.
Be careful if you use my solution. Using the combined JS and PHP SDK, the AccessToken seems to lose it's persistence. the problem I have is getting the following error:
OAuthException: An active access token must be used to query information about the current user.
After reading, I discovered that this was a but that got fixed on pull 48 of the SDK:
https://github.com/facebook/facebook-php-sdk/pull/48
That partially helped but now I get this error:
OAuthException: Error validating access token: Session has expired at unix time
Summary: The combo SDK seems to work for solving your problems but the API obviously behaves buggy. I'm continuing my research and will post when I get resolutions.
Looking forward to hear what others have done to solve this matter
This is my first question here, so apologies if asking something trivial - though I didn't find an answer after an hour of digging.
I have a PHP website which needs to connect with a Facebook page in the following method:
On certain triggers, the site needs to post to the page's wall as the page itself. This has to be done automatically, even if no user session is available (e.g. if someone uses the site without actually having a Facebook account).
I found solutions using extended token expiration times (offline access), but in those cases, the post will always appear as the site admin (me, in this case). However, I need the post to be written by the page. When using Facebook as the page, I have no right to request an offline access token.
Is this even possible at the moment?
You need to cache the page access token on your server and use that to make the updates - the page login is pretty well documented here: https://developers.facebook.com/docs/authentication/pages/ -
Store the access_token in your server-side code and use that token to post as the page - the page access token won't expire if you have a long-expiry access token for the page admin, unless the user stops being an admin of the page, removes the app, etc.
My goal is to use Facebook Login on a website with as few api calls as possible. I don't want to use any server-sided facebook api call on sites that don't interact with facebook at all. That means I only want to use api calls for logging in and publishing things. However I don't want to use Facebooks offline_access permission flag.
Here are some thoughts:
Use own sessions for the site instead of relying on Facebook sessions. So a user stays logged in even if the Facebook session token is expired. Otherwise the user will be logged out as soon as the token is expired and needs to click login again to get a new token or Facebooks JS SDK will auto-login but this still isn't perfect because the user will see the page in "logged-out state" and it will refresh as soon as the JS SDK got a new token. No-JS users have to click the Login button again.
Cache Facebook user data in database or memcache. Use Facebook API Subscription to keep the database updated. -> No need to ask the API for changes in the user's Facebook profile.
Problem:
While using own sessions the Facebook session token might be expired when trying to publish sth. on Facebook. So you have to abort the current script and get a new token by redirecting the user to Facebook or using the JS SDK. Then continue the script. This is pain.
My thought on how to solve the problem:
Instead of publishing things on Facebook using PHP you can also do this in Javascript. If the token is expired just get a new one using JS which does not require the page to be reloaded nor the page to be redirected to Facebook. However I want to support users with disabled Javascript and then this is not possible.
In my opinion the only useful approach is to use own sessions with cached user data and keep the people logged in even if the Facebook token is expired. But redirect them to Facebook and back as soon as Facebook interaction is required. This way the user won't see the page in "logged-out state", no Javascript is required and the user will be only redirected to Facebook Login if it's really needed. For Javascript users the Facebook JS SDK will renew the token (which is then stored in a cookie) without the user noticing this as the user is still logged in (using the own session handling).
What do you think? Is there another (better?) approach to do this? My last point quite seems doable but isn't there an easier way? Thanks.
I had this exact same issue when creating an authentication system for my website. Like Elad Lachmi said, you can run FB.login on each page load - but this will cause a quick popup flash (gets very annoying).
My solution actually ended up requiring me to use offline_access. It seems that there is just no other appropriate way to keep the user logged in if they leave your site for a couple of hours.
Here's the (potential) catch:
Log the user in via Facebook without offline access
On each page, use jQuery to write an iframe to a specially formed URL which uses FB.getLoginStatus, and set it to refresh every 15 minutes
This combination should, every 15 minutes, fetch the most up-to-date user session ID from Facebook. It will cause it to be updated when it is close to expiration.
Please note that I haven't tried this, and it may be against the Facebook ToS. But it is really the only solution without using offline_access if you want to keep a user logged in for more than 2 hours without requiring them to refresh a page.
The website I am developing requires authentication before you can access anything, but I want to allow users to share information with facebook once they have logged in. When I use the standard facebook sharer.php link it pulls up the data from the login page, not the page with the data the user would want to share.
http://www.facebook.com/sharer.php?u=<url to share>&t=<title of content>
Is there any way to allow Facebook to see the data behind the authentication?
I found a temporary work around:
[http://forum.developers.facebook.net/viewtopic.php?pid=178614]
In the last post on this page the user talks about how he used static pages with redirects so that the facebook sharer would find the proper data, and the user would be redirected to the page they expected. Doesn't seem to be the best way, but it works for now.