CS and the City

  • rss
  • Home
  • Resume

Android, the Facebook SDK, SSO, and You

Sean Lynch | July 1, 2011

There seems to be near universal misunderstanding of Facebook’s Android SDK and the single sign on (SSO) feature Facebook added late last year. I originally wrote a response on Stack Overflow detailing the fix but I didn’t realize the connection to SSO at the time. I’m hoping this post can summarize the problem and solutions for the mass of similarly confused developers who get stuck here.

Background

In November 2010, Facebook announced that they’re enabling Single Sign On in the Android SDK. Applications that take advantage of this feature will allow users to skip re-entering credentials and dive right into the action. What they do not mention is that SSO isn’t a feature developers opt into, it’s actually on by default. However, it only changes the way the Facebook SDK works IF the Facebook application is also installed. This causes the problem that most developers (including myself) see when they first set out to build an application.

The issue

The typical description of the problem goes something like this: You’ve downloaded the SDK and your application is running perfectly with shiny new Facebook authentication on the Android emulator. But when you deploy it to a device, it no longer works. The app loads but the Facebook login dialog disappears instantly. If you’re more familiar with Android development than I was at the start, you start up your copy of adb logcat while your application is running and you see logs that look something like this:

D/Facebook-authorize( 2194): Login failed: invalid_key
W/System.err( 2194): com.facebook.android.FacebookError: invalid_key

At this point, you Google for solutions to the issue and quickly start pulling out your hair out at the number of people reporting this issue with no apparent fix. The problem is actually very simple, though not immediately obvious. The problem is that, when deploying your application to the device after developing on the emulator, you’ve inadvertently and implicitly enabled Single Sign On because your device has the Facebook application installed. This is why one of the reported fixes is to uninstall the Facebook application. Your emulator does not have the Facebook application installed (though the Facebook SDK includes it if you want to install it), but your device does, thus triggering the SSO code. And SSO has some special configuration requirements that non-SSO does not which causes the invalid_key error above.

Fixing the Issue

There are a few different ways to tackle this problem.

1. The ugly: Uninstall the Facebook app

Don’t do this. You’re not going to be able to ask your users to do the same anyway. The only reason this works is because the SSO functionality is triggered by the presence of the Facebook application and this simply removes the possibility of using SSO completely, which is also a crappy user experience.

2. The bad: Opt-out of Single Sign On

If you want, you can actually have your application skip SSO completely. You probably don’t want to do this, but it’s a reasonable solution if you’re convinced you do. To do so, you need to modify the code calling Facebook to specify that you want to handle auth on your own. You do this by passing FORCE_DIALOG_AUTH value into the authorize method’s activityCode parameter.

3. The good: Set up Single Sign On properly (recommended)

Unless you have a good reason not to, you should set up SSO. It’s a bit more work, but it’s the best experience for your users.

Buried in the Facebook documentation is a mention about hash codes. Although it’s not obvious in the documentation, Single Sign On requires applications to provide a Key Hash or certificate (I use them interchangeably) of the signature used to sign the application to Facebook. This is used as part of the validation with SSO. When applications are built by the Android development tools, they’re automatically signed using a debug keystore. You need to use this keystore to generate the certificate. Details about the Debug keystore are available in the Android Documentation – Signing Applications.

In order to provide Facebook with information about the signature, you need to pull it from the keystore. On OSX, you do this in the terminal with the following command:

keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64

This generates a short string of characters (which may include characters such as ‘=’ or ‘/’) that identify the signature. This is the certificate or Key Hash as Facebook calls it. Once you have this, you need to give it to Facebook.

Find your application on Facebook’s Developer page (or create a new one if you haven’t set one up already). Once you’re in the application summary page, choose ‘Edit’ on the Settings banner and then pick ‘Mobile’ on the left-hand side. Under the Android section, you’ll see a box for Key Hash. Paste the certificate string from the command above into this box and hit save. Give it a few minutes to propagate and try running your application again. The invalid_key errors should disappear. Keep in mind, when you sign your application for distribution, you’ll have to generate another certificate like you do here and provide that as well.

SSO Weirdities

SSO isn’t completely smooth sailing though, there are a few issues to watch out for.

1. authorize() always shows a page, even if the user is authorized
As far as I’m concerned, this is a bug in the SDK. The workaround is to store the token after authorizing the first time and simply use that instead of calling authorize again while isSessionValid() is true (Stack Overflow has a great example of how to save the access_token using Android’s PreferenceManager). However, unless you want to request an offline_access token, the token will only be valid every 24 hours.

2. Different Access Token formats
The token received from SSO applications are of a different format. There’s a bug open on Facebook’s bug tracker about this, but they can’t seem to track it down despite it being easy to replicate. This isn’t a huge issue, but unfortunately, you won’t be able to parse the user ID of the Facebook User out of the SSO token the way you can from standard access token format

3. UI is inconsistent between SSO and non-SSO
Non-SSO uses a nicer pop-over dialog to show the authentication panel while the SSO panel slides in from the right (and back out to the right after the user finishes). There doesn’t appear to be a way to change this UI, at least without hacking the SDK code directly.

Categories
Android, Facebook, Google
Tags
android, facebook, java, sdk
Comments rss
Comments rss
Trackback
Trackback

« Checkpointing How Microsoft and Nokia can avoid smothering their spark »

21 responses

It is a long time since I looked at the

Peter | July 1, 2011

It is a long time since I looked at the Facebook integration in my my app but it does suffer from ” authorize() always shows a page, even if the user is authorized”, is this really avoidable? I tried to follow the examples in the SDK but they seem to suffer from it too. I really wish it was simple adding 2 or 3 lines of code to login.

It's only avoidable by storing the access_token until it expires

Sean Lynch | July 1, 2011

It’s only avoidable by storing the access_token until it expires (which, currently, is 24 hours unless you’re requesting offline_access).

24 hours is not really all that long and offline

Peter | July 2, 2011

24 hours is not really all that long and offline access is a scary sounding permission (to me anyway).
Sso is broken if you ask me but Facebook ignored the bug I saw open about it. I imagine Android will have some amazing Google + integration in a little while.

Thank you so much. You just save my day.

Leiko | August 16, 2011

Thank you so much. You just save my day.

gracias pero no es lo que busco

blogeton | August 16, 2011

gracias pero no es lo que busco

Hello Sean, I am indeed very grateful to you for

Rashmi B | August 22, 2011

Hello Sean, I am indeed very grateful to you for putting up this info in your blog.

I have been facing this error since 2 days. But to my surprise, I have put the key hash in my app as well as tried the “UGLY” method. UGLy approach seems to work fine, everytime i uninstall the updates for fb and run my code, and it just works perfect. But when I delete the app and say reinstall the fb from the market, it will obviously generate the new version. When I click on “Login to fb” it says authorize and comes back to the home screen. leaving a message in the logcat saying “invalid key error”. What else do I need to check buddy?

My development environment is a Windows Vista and I read

Andre Leitao | August 22, 2011

My development environment is a Windows Vista and I read in stackoverflow thats it may causing the still existing “Invalid key error”.

What you have to say about this? I really need to install cygwin to be able to correctly generate the key hash (I am already able to generate it, including with the password prompt, but the error remains)

Hi, Im on a Mac and whenever i run the

Kartik | August 24, 2011

Hi, Im on a Mac and whenever i run the following command

keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64

I get an error sayin “Illegal option: -exportcert”

Please help

Thank so much. I should have found this blog a

Anh Le Tuan | September 5, 2011

Thank so much. I should have found this blog a week ago.

Thnks it was really helpful!!!!

Marceliz | September 7, 2011

Thnks it was really helpful!!!!

Hello frind, i am using same what u explain here ..it's

Shubham | September 8, 2011

Hello frind,

i am using same what u explain here ..it’s totally good one..but at end it asking for keystore file password..whatever i give it responding me same key .. ga0RGNYHvNM5d0SLGQfpQWAPGJ8= ..can u suggest me what i m doing wrong/missing.

pls mail me at shubhampatni86@gmail.com if possible
thanks again

Fantastic article, you saved me a headache and I owe

andorraclaim | November 29, 2011

Fantastic article, you saved me a headache and I owe you a beer. Salud!

Hi. nice post. But i am facing a problem that

Victor Pease | December 15, 2011

Hi. nice post.

But i am facing a problem that i think its related to this. My application is working fine as long as i login with the facebook user who owns the app. If i try to login with a different FB user, the SSO form pop up but shows and error “An error ocurred. Please try again later”.

If i add the user to a role in my FB app, this error disappear but i want to be able to provide access to any FB user. What am i missing here?

[...] to Sean Lynch for posting about this and saving

Enabling PhoneGap apps to access Facebook SSO – BigGek | December 23, 2011

[...] to Sean Lynch for posting about this and saving me from throwing my computer through a window when I got this [...]

I have followed your method but still not working, it

rooban abraham | January 3, 2012

I have followed your method but still not working, it working in s2 but this was not working in Sony Ericsson mobiles. what can do for solving this? pls ,

When i login to Facebook for authentication this does not give any response in Sony Ericsson mobiles. simply closed the dialog with out any response.

Unfortunately, I'm not sure why their SDK would not work

Sean | January 4, 2012

Unfortunately, I’m not sure why their SDK would not work on Sony Ericsson devices. Can you confirm it works on other phones? Do you actually get logging output from your Sony Ericsson devices?

I would contact Facebook. If you can get the same code working on non-SE devices, they might have a bug.

Yes all other device like S2, even in emulator

rooban abraham | January 5, 2012

Yes all other device like S2, even in emulator which was working but in SE mobile alone i am not getting any response when i attempting authenticate user. this simply opening the loading dialog and after some time it disappear without any log.

Thanks very much your are a lifesaver

Hakem Zaied | January 6, 2012

Thanks very much your are a lifesaver

Extremly usefull info! Thanks a lot!

Dmitriy Krivoshey | January 23, 2012

Extremly usefull info! Thanks a lot!

Bravo!! Thanks a lot!! This problem frustrated me really but your

AbdulFattah Popoola | February 12, 2012

Bravo!!

Thanks a lot!! This problem frustrated me really but your article was spot on!

Thanks once again :) .

But not able to logout from our applicaton. i followed

Nikhil.K | February 17, 2012

But not able to logout from our applicaton. i followed above steps..Login successfully.

please show me a sample for logout or please explain logout and something like delete the permissions

Thanks in advance

Leave a comment

You can use these tags : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Navigation

  • Business
    • Apple
    • Facebook
    • Google
    • Microsoft
    • Yahoo
  • Canada
  • Copyleft
  • Development
    • Android
    • Interfaces
    • Protocols
    • Python
  • How-to
  • Reviews
  • School
  • Technology
    • Gadgets
    • Software
  • Truthiness

Search