Oauth 2.0 – facebook changes and cookie problem in Codeigniter

Lately Facebook forced all developers to use oauth 2.0 by setting by default oauth:true in FB.init();

The most important changes are:

  • Cookie name is now ‘fbsr_’. not ‘fbs_’
  • Cookie has more changes than you think ;o)
  • perms are now scope – so now instead of writing in e.g. login button perms=”email,publish_stream” you write scope=”email,publish_stream”
  • to get access_token we need to have magic code first
  • if you’re using some client-side scripting too, then instead of FB.getSession use FB.getAuthResponse(thanks ajquick)

Things you need to change are probably similar for other frameworks if you don’t use SDK. I tested this solution for CodeIgniter 2.x. But it should work for earlier ver too.

This is quick and dirty, and it was tested for authentication. If you have problem only with cookie function skip first two steps and check if new function works for you.

If you are making some rewriting now I encourage you to rewrite code so you can use their SDK. Maybe next time, after so many changes the only thing you will have to do is update SDK.

First of all we need to add to our FB.init() oauth:true. Although it’s now default, but you never know if they change it back. Just for fun.

Next, change in your login link/button word perms to scope. Otherwise you will get : “OAuth2 specification states that ‘perms’ should now be called ‘scope’. Please update.” error. It’s just changing name, nothing else.

Then if your class use cookie you need to change it name from ‘fbs_’ to ‘fbsr_’. But there was more changes. If you use function get_facebook_cookie() like one below (as I saw in the web many people are):

change it to something like that:

If you use in other places that function, probably you also use it for getting session_key, maybe for logout url. Anyway, it seems that session_key is now magic code, so add to function

just before return if you use that in many places and you are so lazy that you don’t want rewrite it there.

The changed cookie function works also for getting list of friends. I’m guessing that will work for all functions that use something like that:

However I recommend to use PHP SDK from facebook, because if they change something again, most likely you will have to update only SDK, and not rewriting your code over and over again.

  • Share it on Twitter
  • Share it on Facebook
  • Share it on Pinterest
  • Share it on Google+
  • Share it on LinkedIn

Comments (2)Add comment

Take part in the discussion

Use pastebin.com or jsfiddle.net to add your code snippets
Your email address will not be published.