/** * Helper file for creating a Facebook session with Flash based applications. * */ package com.spice.clove.plugin.facebook.utils { import com.facebook.Facebook; import com.facebook.events.FacebookEvent; import com.facebook.facebook_internal; import com.facebook.session.IFacebookSession; import com.facebook.session.JSSession; import com.facebook.session.WebSession; import com.spice.recycle.events.DisposableEventDispatcher; import flash.display.LoaderInfo; import flash.net.SharedObject; import flash.system.Capabilities; /** * The FacebookSessionUtil class provides a convenient way to create * or continue a Facebook session. This class handles many of the tasks that * you would otherwise have to handle manually. The FacebookSessionUtil class * constructor: * * * @see com.facebook.session * @see http://wiki.developers.facebook.com/index.php/Session_Secret */ public class BetterFacebookSessionUtil extends DisposableEventDispatcher{ /** * The instance of the Facebook class created by the constructor. */ public var facebook:Facebook; protected var api_key:String; protected var secret:String; protected var loaderInfo:LoaderInfo; protected var session_key:String; /** @private */ protected var _activeSession:IFacebookSession; /** * The constructor creates a new session of the appropriate type. * See the FacebookSessionUtil class description for detailed information * about the constructor. * * @param api_key Your application's API key. * @param secret Your application's secret key. If this parameter is passed * a value of null, the constructor looks for a special * session secret stored in the fb_sig_ss property of the * loaderInfo object. For web sessions, even if you pass a * non-null value for this parameter, the constructor will always look * for a session secret and use that instead of the value that you pass * for this parameter. * @param loaderInfo An object of type LoaderInfo that provides information * about the loaded SWF file. */ public function BetterFacebookSessionUtil(api_key:String, secret:String, loaderInfo:LoaderInfo) { this.secret = secret == null?loaderInfo.parameters.fb_sig_ss:secret; this.api_key = api_key; this.loaderInfo = loaderInfo; var savedCreds:SharedObject = getStoredSession(); //See if we have a saved session_key if (savedCreds.data.session_key) { session_key = savedCreds.data.session_key; } var flashVars:Object = loaderInfo != null?loaderInfo.parameters:{}; //Use the session provided by Facebook, if one exists if (flashVars.fb_sig_session_key != null) { session_key = flashVars.fb_sig_session_key; } if ((loaderInfo && loaderInfo.url.slice(0, 5) == "file:") || Capabilities.playerType == "Desktop") { //desktop application _activeSession = new AIRDesktopSession(api_key, this.secret); } else if(flashVars.fb_sig_ss && flashVars.fb_sig_api_key && flashVars.fb_sig_session_key) { //Web application _activeSession = new WebSession(flashVars.fb_sig_api_key, flashVars.fb_sig_ss, flashVars.fb_sig_session_key); (_activeSession as WebSession).facebook_internal::_uid = flashVars.fb_sig_user; } else if(flashVars.as_app_name) { //jsBridge application _activeSession = new JSSession(api_key, flashVars.as_app_name); } else { //could not determine facebook connection type, so just use DesktopSession _activeSession = new AIRDesktopSession(api_key, secret); } _activeSession.session_key = session_key; _activeSession.addEventListener(FacebookEvent.VERIFYING_SESSION, onVerifyingSession); //Create our facebook instance facebook = new Facebook(); facebook.addEventListener(FacebookEvent.WAITING_FOR_LOGIN, handleWaitingForLogin); facebook.addEventListener(FacebookEvent.CONNECT, onFacebookReady); facebook.startSession(_activeSession); } /** * Purges user login data. * Cleans up sharedObject data. */ public function logout():void { getStoredSession().clear(); getStoredSession().flush(); facebook.logout(); } public function onVerifyingSession(event:FacebookEvent):void { dispatchEvent(event); } /** * The active session created by the constructor. */ public function get activeSession():IFacebookSession { return _activeSession; } public function login(offline_access:Boolean = true):void { facebook.login(offline_access); } protected function handleWaitingForLogin(event:FacebookEvent):void { dispatchEvent(event); } /** * Call first to check an established login. */ public function verifySession():void { _activeSession.verifySession(); } /** * if there is no prior login session, validate create a new session */ public function validateLogin():void { facebook.refreshSession(); } protected function onVerifyLogin(event:FacebookEvent):void { _activeSession.removeEventListener(FacebookEvent.CONNECT, onVerifyLogin); if (event.success) { onFacebookReady(null); dispatchEvent(new FacebookEvent(FacebookEvent.CONNECT, false, false, true)); } else { dispatchEvent(new FacebookEvent(FacebookEvent.CONNECT, false, false, false)); } } /** * get the stored session for the set api */ protected function getStoredSession():SharedObject { return SharedObject.getLocal(api_key+"_stored_session"); } protected function onWaitingForLogin(event:FacebookEvent):void { dispatchEvent(event); } /** * Called when the facebook connection is ready. */ protected function onFacebookReady(event:FacebookEvent):void { if (facebook.session_key) { var storedSession:SharedObject = getStoredSession(); storedSession.data.session_key = facebook.session_key; storedSession.data.stored_secret = facebook.secret; storedSession.flush(3000); } if (event) { dispatchEvent(event); } } } }