First free Facebook Single Sign-On (SSO) Adobe AIR native extension for Android
Overview
Our GCM extension and tutorial have been available for a while now and people have requested a number of updates. Some items on the list include adding a sound flag, incorporating notification icons into the extension init, and more. Don’t worry, these are coming! For now, here is a new extension we created that will help fellow AIR developers improve their apps and games.
Until recently, our app “Sketch Guess” utilized Web View for logging people in with Facebook credentials. It works well and looks decent. However, it can be cumbersome to users who are accustomed to seamless entry into FB integrated apps and games when they are already logged into Facebook mobile. That’s where SSO comes in. It allows your app to automatically use the login token from the Facebook application if your app was authorized by the user.
While it is quite easy to implement in native Android apps, we were not able to find a ready-to-go dedicated solution on the web. Therefore we had to create our own extension.
It was written for Adobe Flex SDK 4.6 + AIR SDK 3.5. Although, I’m sure it will probably work with the earlier versions of AIR (but not too early). My weapon of choice in developing AIR apps is Intellij 10 to 12. Therefore, I won’t be providing exact procedures to link to “.ane” when you setup the project since that varies from environment to environment.
Note: Facebook requires SSO for apps in their App Center. In other words, you cannot be listed in their marketplace unless your application implements SSO.
Download the ANE and example here and let’s begin…
Application setup in Facebook to use Single Sign-On (SSO)
1. Go to https://developers.facebook.com/apps/, click ‘Create New App’ and enter basic information such as the app name and a unique namespace.
2. Once the app is created, click ‘Edit’. Note the app ID shown at the top of the dashboard page. You’ll need that ID to use for your SSO login.
3. Check the ‘Native Android App’ section at the bottom of the page. Enter your package name. In our case it was “air.com.afterisk.sketchguessfree”.
Note: Don’t forget to add “air.” before your package name (our package name was actually “com.afterisk.sketchguessfree”) or it won’t work!
4 . Enter your application Class Name. For Adobe AIR applications, it is always “.AppEntry”, so in our case it was “air.com.afterisk.sketchguessfree.AppEntry”
5. Mark “Facebook Login” as enabled.
6. Now, enter your Key Hash. This is a security measure from Facebook to determine if the app that is trying to access SSO on your behalf is actually the app you created.
To create a certificate, refer to this Adobe DevNet article:
http://www.adobe.com/devnet/air/articles/packaging-air-apps-android.html
Note: If you already have a .p12 that you are using to sign your .apk and you don’t know the hash key, then next section is for you, otherwise skip to implementation part.
Extracting the Key Hash from .p12 key
1. Open Terminal or Command line and navigate to where your .p12 key is.
2. Type in: “keytool -v -list -keystore mycert.p12 -storetype pkcs12” where mycert.p12 is the filename of your .p12 key.
3. Enter keystore password (the one you used when exported .p12 key).
4 . Copy sha1 fingerprint signature bytes text.
5. The bytes at sha1 fingerprint signature are needed to write the “sha1.bin” file. You can use a hexadecimal editor to paste the bytes you copied. Afterwards, save the file as “sha1.bin”.
6. Open terminal again and type in: “openssl base64 -in sha1.bin -out base64.txt”.
7. The resulting “base64.txt” will contain the Key Hash that is needed for Facebook.
Great and simple hexadecimal editor for mac: HexFiend
OpenSSL should be preinstalled on mac, and here is the link for Windows version.
Extension implementation
Now, that we are done with Facebook’s “easy” setup, we can implement our extension into the app. If you never used Facebook Actionscript API by Adobe before, please download it here and familiarize yourself with it. The inner workings of that API are not in the scope of this article. With that said, lets continue.
Add extension to your application.xml:
<extensions> <extensionID>com.afterisk.shared.android.AndroidSSO</extensionID> </extensions>
Add new activity to your application tag:
<application android:enabled="true" android:hardwareAccelerated="true"> <activity android:name="com.afterisk.shared.android.sso.AndroidSSOActivity"/> </application>
The activity above is what allows us to send user back from successful (or not) sign-on from Facebook.
Next up is integration of the AS3 part of the SSO interface we’ve created with your app. The interface has 2 parameters: your application Facebook ID (from step 2 in FB app setup) and a flag which indicates whether you’d like to use the Facebook native authorization window or not. Some people might prefer to fall back to Web View login if there is no authorization token.
SSO implementation example (this example is also a part of the downloadable zip):
/** * Created by IntelliJ IDEA. * User: Afterisk Tech * Date: 2/22/13 * Time: 9:17 AM * To change this template use File | Settings | File Templates. */ package { import com.afterisk.shared.ane.lib.AndroidSSOEvent; import com.afterisk.shared.ane.lib.AndroidSSOInterface; import flash.display.Sprite; public class SSOExample extends Sprite { private static var _ssoi:AndroidSSOInterface; //The key from your application page on Facebook developer portal (http://developers.facebook.com) public static const FACEBOOK_APPLICATION_KEY:String = "yourkeyhere"; public function SSOExample():void { _ssoi = new AndroidSSOInterface(); //Check if Facebook App is installed on the device _ssoi.addEventListener(AndroidSSOEvent.INIT_CHECK_COMPLETE, handleInstallCheck, false, 0, true); _ssoi.checkIfInstalled(); } private static function handleInstallCheck(e:AndroidSSOEvent):void { _ssoi.removeEventListener(AndroidSSOEvent.INIT_CHECK_COMPLETE, handleInstallCheck); if(e.facebookInstalled) { //If Facebook app is intalled add listeners to the interface and start the authorization process _ssoi.addEventListener(AndroidSSOEvent.NO_SESSION, handleAndroidSSOStatus, false, 0, true); _ssoi.addEventListener(AndroidSSOEvent.AUTHORIZED, handleAndroidSSOStatus, false, 0, true); _ssoi.addEventListener(AndroidSSOEvent.CANCELED, handleAndroidSSOStatus, false, 0, true); _ssoi.addEventListener(AndroidSSOEvent.ERROR, handleAndroidSSOStatus, false, 0, true); //Authorization process, setting 2nd parameter to true allows you to use Facebook //native authorization UI _ssoi.authorize(FACEBOOK_APPLICATION_KEY, true); } else { //Fall back to web view login if Facebook application is not installed on the device } } private static function handleAndroidSSOStatus(e:AndroidSSOEvent):void { if(e.type == AndroidSSOEvent.AUTHORIZED) { //The application has been authorized, use token to login using your Facebook Actionscript API from Adobe //http://code.google.com/p/facebook-actionscript-api/ trace(e.accessToken); trace(e.expiresIn); } else { //the authorization was not succesful, retry, fall back to Web View login or do something else. trace("Event: " + e.type); } } } }
That is it! You are now ready to use Facebook Single Sign-On with your own application.
The example of fully working Facebook SSO implementation on Android can be seen in our drawing game:
Good luck!
Download
Again, here is the Facebook SSO Extension and Example link.
I really appreciate this article, thank you.
Can you add more instructions in regards to creating the Key Hash? I’m trying to use command line (with open ssl.. somehow), but if I do what the instructions say and go to where my .p12 file is saved and run that code it just tells me:
“‘keytool’ is not recognized as an operable program or batch file.”
I know that keytool is part of java but I’m confused how to put it all together with this.
I used this code before to make what I think is a working Key, but I assume that somehow I have to mix all this together to use my .p12 file..
C:\Program Files\Java\jre7\bin>keytool -expo
\user\.android\myKeyStore | C:\openssl-0.9.8
:\openssl-0.9.8k_WIN32\bin\openssl enc -a -e
I’m sorry i’m using mac for development, i don’t even own a pc, so i wouldnt be able to provide any info on how to do it in windows.
it works fine till handleInstallCheck() method,and i have installed facebookapp
but handleAndroidSSOStatus() mehod is not called back.
my enviroment — flash buidler4.7 air 3.7
device — android
after several hours,i find something missing.
I did not add this to xxx-app.xml file.
Now it works.
but i do not see the log out interface,don’t we need one ?
It’s not a facebook login, its an authorization for app, so you don’t need to log out. If user decides to de-authorize the app they’ll do it via facebook tools.
Hi guys,
really nice work. Is the extension and the sample still available?
The download link doesn’t seem to work anymore.
many thanks
GA
the site was down, indeed. should be fine now! 🙂
Hi guys,
I notice that the first download link is broken, but the download link at the end works 😀
Everything works fine until I try to build it and it keeps saying “extension was not found for the target platform”. After I grind a lot and I finally find that the ANE specify the platform to be Android-ARM, but I am not able to set it using flashdevelop. Any clues on what should I do, or the ANE support android platform too?
Many thanks
Sorry, but this extension works only for android, ios has a completely different way of authorizing the facebook.
Sorry for the misunderstanding. I am now able to have it work for android.
But when i call _ssoi.authorize(FACEBOOK_APP_ID, true)
it give me 2 response, first a AndroidSSOEvent.NO_SESSION
Then it returns AndroidSSOEvent.AUTHORIZED again
Is it an intended result or am I missing something?
Thanks
Hi, I’m testing your extension to see if we can use it in our application, but I get the following error:
AndroidSSOInterface Status: Something is wrong in fb UI.
AndroidSSOInterface Code: dialogError
This is then followed by a AndroidSSOEvent.ERROR event.
Can you tell me a bit more about when this error occurs? This happens on an HTC One with Android 4.2.2. The Facebook app is reported as installed, and is working properly…
The error happens on Facebook side, when the “DialogListener” which is part of their API throws the “DialogError”. All that is on java side of things, outside of the extension or your app. Why it throws that error? Possibly “authorization” dialog (popup) cannot show for some reason…To track this you need to try and debug your app using “logcat”, that’s where you (and i’m not promising) can see the details of what is going on on FB or Android OS end.
Updating the Facebook app on the HCT One helped 🙂
I downloaded the files you supplied and when i tried to run it i am getting the following errors:
C:\afterrisk sso\android_sso_extension\src\SSOExample.as, Line 29 1046: Type was not found or was not a compile-time constant: AndroidSSOEvent.
C:\afterrisk sso\android_sso_extension\src\SSOExample.as, Line 50 1046: Type was not found or was not a compile-time constant: AndroidSSOEvent.
I asume its something got to do with importing the AndroidSSOEvent.
What am i overlooking? Help would be appreciated.
What you see is a result of the extension packages not being imported. Make sure you import ane correctly, depending on your IDE it could be a different process.
Of course I simply forgot to import them. however when i import the .ane file to the fla file I ma getting the following error:
Test Movie terminated.
The content cannot be loaded because there was a problem loading an extension: Error: Extension namespace is invalid for C:\Users\Mark\AppData\Local\Temp\Tmp_ANE_File_Unzipped_Packages\androidsso.ane.
I also noticed you mention this is just for authorizing the fb app. is this sufficient for accessing user data? . have a login working using the web view but would prefer the user dint have to go to the hastle of entering details.
Ok so the first issue has nothing to do with the SSO extension The extension version identified on the top of your xml is not compatible with the AIR libraries you’re using to compile the app. Please refer to adobe documentation. As far as user data goes…This extension is built to allow the streamlined login using FB app users might have installed and logged in to on their device. The user data access will require you to use public AIR Facebook API and request permissions for your app.
Hey this is excellent- Thank you soo much!
We have a problem with GoViral and they are not responding to our support question.
So I am wondering if we can do this via your ANE? If an android device has the Facebook app installed on it then our Game has problems because the Facebook app doesn’t share the same cookies as webview- So if we need to display other FB features in our game in webview the user is not logged in to Facebook.
However if we do not have the FB app installed on the android device then our game calls the webview login for FB and all features work fine.
If we can somehow use this function to ONLY call the webview window for login even IF the FB app is installed then the problem is solved :
mFacebook.authorize(mActivity, permissions, Facebook.FORCE_DIALOG_AUTH,
new LoginDialogListener());
Can we do this using your ane?
Unfortunately no, you cannot. This ANE is designed to use facebook SSO whenever possible, if you would like to always use the web view login you can just use the facebook SDK for AIR: https://code.google.com/p/facebook-actionscript-api/
Ah, Yea I tried using that but it functions poorly on average/low connection signals.
Do you know if there is a way to achieve the user remaining logged in after signing in if the FB app is installed?
Our problem is if the FB app is called for login then the user can’t view other FB pages within our app. But if the FB app isn’t installed on the android device then it calls the webview window for login and all functions ok.
I don’t want to abort my hard work so far but I feel there is no work-around.
You can keep user logged in. That’s what SSO is for, but it needs to use the app. You can’t login from your app. It has to be either the FB app or the webview with their login form.
Yep, that’s what we’re doing. We are logging in via FB app or Webview (which ever is prompted by native process) but once signed in only the Webview login allows the user to remain logged in and visit, share, like etc. If the FB app is present on the device and native process calls that to login with everything from that process on is like we are not even logged in- Although the user has successfully logged in.
Thank you for the great extension!
A small tip – I probably did something wrong when I followed the instruction on how to get my key hash and received an error when I tried to log in from my app. But Facebook made it a lot more simple for me to fix – they wrote my key in the error message and I just went and replaced it in my app settings. I think trying to log in once and get the error to know what your key is, is much easier. Thanks again.
No problem, oz. Glad we could help!
I don’t know why i can’t make a login with facebook,.. the facebook page opens but nothing else happens… what could be making wrong
Sorry, but “Download the ANE and example here” link is already broken (404). Can you upload elsewhere, pls?
The link was updated, please try again. Sorry for the inconvenience.
This may be a noobish question, but could someone please tell me how to make this work in Flash CS6 IDE? I have a mobile AIR project and setting the .as file as a main class kinda works, but how do I trace/pass results from it? I cannot access anything that’s on stage from the provided .as file.
OK, I’ve managed to make it work, but passing the token to the FacebookMobile, along with app id and handler return null objects.
Hi, both download links are broken,regards
Server was down, due to some hosting issues. It should work now.