AS3: Facebook Authentication Example


/ Published in: ActionScript 3
Save to your folder(s)

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!


Copy this code and paste it in your HTML
  1. import com.facebook.commands.friends.GetFriends;
  2. import com.facebook.data.users.FacebookUser;
  3. import com.facebook.data.friends.GetFriendsData;
  4. import com.facebook.events.FacebookEvent;
  5. import com.facebook.Facebook;
  6. import com.facebook.net.FacebookCall;
  7. import com.facebook.utils.FacebookSessionUtil;
  8. import flash.display.Stage;
  9. import flash.display.LoaderInfo;
  10. import mx.controls.Alert;
  11.  
  12. private var API_KEY:String = "API_KEY";
  13. private var SECRET_KEY:String = "SECRET_KEY";
  14.  
  15. private var fbook:Facebook;
  16. private var fbLoaderInfo:LoaderInfo;
  17. private var fbSession:FacebookSessionUtil;
  18. private var currentMethod:Function;
  19.  
  20. private function initApp():void
  21. {
  22. fbLoaderInfo = stage.loaderInfo;
  23.  
  24. getFriends();
  25. }
  26.  
  27. /**************************************
  28.  * Actions
  29. **************************************/
  30. private function getFriends():void
  31. {
  32. //If the user is not logged in, force one
  33. if( !isAuthenticated() ){ fbLogin( arguments.callee ); return }
  34.  
  35. var call:FacebookCall = fbook.post( new GetFriends( ) );
  36. call.addEventListener( FacebookEvent.COMPLETE, getFriendsCompleteHandler, false, 0, true );
  37. }
  38.  
  39. private function getFriendsCompleteHandler( e:FacebookEvent ):void
  40. {
  41. if( e.error ) trace( "getFriendsCompleteHandler: " + e.error);
  42.  
  43. var friendsData:GetFriendsData = e.data as GetFriendsData;
  44.  
  45. //Iterate through the albumsResponseData.albumCollection to get the titles
  46. for( var i:uint = 0; i < friendsData.friends.length; i++ ){
  47. var friendID:Number = friendsData.friends.getItemAt(i).uid;
  48. var facebookUser:FacebookUser = friendsData.friends.getUserById( friendID );
  49. trace( "Facebook User Name: " + facebookUser.name );
  50. }
  51.  
  52. }
  53.  
  54.  
  55. /**************************************
  56.  * Facebook Authentication
  57. **************************************/
  58. private function fbLogin( method:Function ):void
  59. {
  60. currentMethod = method;
  61.  
  62. fbSession = new FacebookSessionUtil( API_KEY, SECRET_KEY, loaderInfo );
  63. fbSession.addEventListener( FacebookEvent.WAITING_FOR_LOGIN, fbWaitingForLoginHandler, false, 0, true );
  64. fbSession.addEventListener( FacebookEvent.CONNECT, fbOnConnectHandler, false, 0, true );
  65. fbook = fbSession.facebook;
  66. fbSession.login();
  67. }
  68.  
  69. private function isAuthenticated():Boolean
  70. {
  71. if( fbSession == null || !fbook.is_connected ) return false;
  72.  
  73. return true;
  74. }
  75.  
  76. /**************************************
  77.  * Facebook Event Handlers
  78. **************************************/
  79. private function fbOnConnectHandler( e:FacebookEvent ):void
  80. {
  81. trace( "You are logged into Facebook" );
  82. //Continue where the user last left off before requiring Authentication
  83. currentMethod();
  84. }
  85.  
  86. private function fbWaitingForLoginHandler( e:FacebookEvent ):void
  87. {
  88. //The user has returned from the log-in page and are now clicking "OK"
  89. var alert:Alert = Alert.show( "Click 'OK' after you've logged in", "Logging In" );
  90. alert.addEventListener( Event.CLOSE, alertCloseHandler, false, 0, true );
  91. alert.addEventListener( Event.CLOSE, fbValidateLogin, false, 0, true );
  92. }
  93.  
  94. private function fbValidateLogin( e:Event ):void
  95. {
  96. fbSession.validateLogin();
  97. }
  98.  
  99. private function alertCloseHandler( e:Event ):void
  100. {
  101. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.