Return to Snippet

Revision: 13345
at April 20, 2009 14:58 by chrisaiv


Updated Code
import com.facebook.commands.friends.GetFriends;
import com.facebook.data.users.FacebookUser;
import com.facebook.data.friends.GetFriendsData;
import com.facebook.events.FacebookEvent;
import com.facebook.Facebook;
import com.facebook.net.FacebookCall;
import com.facebook.utils.FacebookSessionUtil;
import flash.display.Stage;
import flash.display.LoaderInfo;
import mx.controls.Alert;
			
private var API_KEY:String = "API_KEY";
private var SECRET_KEY:String = "SECRET_KEY";

private var fbook:Facebook;
private var fbLoaderInfo:LoaderInfo;
private var fbSession:FacebookSessionUtil;
private var currentMethod:Function;

private function initApp():void
{
	fbLoaderInfo = stage.loaderInfo;

	getFriends();						
}

/**************************************
 * Actions
**************************************/
private function getFriends():void
{
	//If the user is not logged in, force one		
	if( !isAuthenticated() ){ fbLogin( arguments.callee ); return }

	var call:FacebookCall = fbook.post( new GetFriends( ) );
		call.addEventListener( FacebookEvent.COMPLETE, getFriendsCompleteHandler, false, 0, true );										
}

private function getFriendsCompleteHandler( e:FacebookEvent ):void
{
	if( e.error ) trace( "getFriendsCompleteHandler: " + e.error);
	
	var friendsData:GetFriendsData = e.data as GetFriendsData;

	//Iterate through the albumsResponseData.albumCollection to get the titles
	for( var i:uint = 0; i < friendsData.friends.length; i++ ){
		var friendID:Number = friendsData.friends.getItemAt(i).uid;
		var facebookUser:FacebookUser = friendsData.friends.getUserById( friendID );
		trace( "Facebook User Name: " + facebookUser.name );
	}
	
}


/**************************************
 * Facebook Authentication
**************************************/
private function fbLogin( method:Function ):void
{				
	currentMethod = method;
	
	fbSession = new FacebookSessionUtil( API_KEY, SECRET_KEY, loaderInfo );
	fbSession.addEventListener( FacebookEvent.WAITING_FOR_LOGIN, fbWaitingForLoginHandler, false, 0, true );
	fbSession.addEventListener( FacebookEvent.CONNECT, fbOnConnectHandler, false, 0, true );				
	fbook = fbSession.facebook;
	fbSession.login();
}
		
private function isAuthenticated():Boolean
{
	if( fbSession == null || !fbook.is_connected ) return false;
	
	return true;
}

/**************************************
 * Facebook Event Handlers
**************************************/
private function fbOnConnectHandler( e:FacebookEvent ):void
{
	trace( "You are logged into Facebook" );
	//Continue where the user last left off before requiring Authentication
	currentMethod();
}

private function fbWaitingForLoginHandler( e:FacebookEvent ):void
{
	//The user has returned from the log-in page and are now clicking "OK"
	var alert:Alert = Alert.show( "Click 'OK' after you've logged in", "Logging In" );
		alert.addEventListener( Event.CLOSE, alertCloseHandler, false, 0, true );
		alert.addEventListener( Event.CLOSE, fbValidateLogin, false, 0, true );
}			

private function fbValidateLogin( e:Event ):void
{
	fbSession.validateLogin();
}

private function alertCloseHandler( e:Event ):void
{
}

Revision: 13344
at April 20, 2009 14:55 by chrisaiv


Initial Code
import com.facebook.data.users.FacebookUser;
			import com.facebook.net.FacebookCall;
			import com.facebook.data.friends.GetFriendsData;
			import com.facebook.commands.friends.GetFriends;

			import mx.controls.Alert;
			import com.facebook.Facebook;
			import com.facebook.events.FacebookEvent;
			import flash.display.Stage;
			import flash.display.LoaderInfo;
			import com.facebook.utils.FacebookSessionUtil;
						
			private var API_KEY:String = "API_KEY";
			private var SECRET_KEY:String = "SECRET_KEY";
			
			private var fbook:Facebook;
			private var fbLoaderInfo:LoaderInfo;
			private var fbSession:FacebookSessionUtil;
			private var currentMethod:Function;
			
			private function initApp():void
			{
				fbLoaderInfo = stage.loaderInfo;

				getFriends();						
			}
			
			/**************************************
			 * Actions
			**************************************/
			private function getFriends():void
			{
				//If the user is not logged in, force one		
				if( !isAuthenticated() ){ fbLogin( arguments.callee ); return }

				var call:FacebookCall = fbook.post( new GetFriends( ) );
					call.addEventListener( FacebookEvent.COMPLETE, getFriendsCompleteHandler, false, 0, true );										
			}
			
			private function getFriendsCompleteHandler( e:FacebookEvent ):void
			{
				if( e.error ) trace( "getFriendsCompleteHandler: " + e.error);
				
				var friendsData:GetFriendsData = e.data as GetFriendsData;

				//Iterate through the albumsResponseData.albumCollection to get the titles
				for( var i:uint = 0; i < friendsData.friends.length; i++ ){
					var friendID:Number = friendsData.friends.getItemAt(i).uid;
					var facebookUser:FacebookUser = friendsData.friends.getUserById( friendID );
					trace( "Facebook User Name: " + facebookUser.name );
				}
				
			}

			
			/**************************************
			 * Facebook Authentication
			**************************************/
			private function fbLogin( method:Function ):void
			{				
				currentMethod = method;
				
				fbSession = new FacebookSessionUtil( API_KEY, SECRET_KEY, loaderInfo );
				fbSession.addEventListener( FacebookEvent.WAITING_FOR_LOGIN, fbWaitingForLoginHandler, false, 0, true );
				fbSession.addEventListener( FacebookEvent.CONNECT, fbOnConnectHandler, false, 0, true );				
				fbook = fbSession.facebook;
				fbSession.login();
			}
					
			private function isAuthenticated():Boolean
			{
				if( fbSession == null || !fbook.is_connected ) return false;
				
				return true;
			}

			/**************************************
			 * Facebook Event Handlers
			**************************************/
			private function fbOnConnectHandler( e:FacebookEvent ):void
			{
				trace( "You are logged into Facebook" );
				//Continue where the user last left off before requiring Authentication
				currentMethod();
			}
			
			private function fbWaitingForLoginHandler( e:FacebookEvent ):void
			{
				//The user has returned from the log-in page and are now clicking "OK"
				var alert:Alert = Alert.show( "Click 'OK' after you've logged in", "Logging In" );
					alert.addEventListener( Event.CLOSE, alertCloseHandler, false, 0, true );
					alert.addEventListener( Event.CLOSE, fbValidateLogin, false, 0, true );
			}			
			
			private function fbValidateLogin( e:Event ):void
			{
				fbSession.validateLogin();
			}
			
			private function alertCloseHandler( e:Event ):void
			{
			}

Initial URL


Initial Description
I wanted an easy way to authenticate a user before making calls to Facebook.  It's not perfect but it certainly makes things easier. Note: This is not a class!

Initial Title
AS3: Facebook Authentication Example

Initial Tags
Flex, facebook

Initial Language
ActionScript 3