Posted By

chrisaiv on 04/20/09


Tagged

Flex as3 facebook


Versions (?)

Who likes this?

6 people have marked this snippet as a favorite

enajenkins
tripleaxis
elliotjameschong
donaldleegraham
tspitzr
dubogii


AS3: Facebook Authentication Example


 / Published in: ActionScript 3
 

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!

  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
Posted By: sytr on September 7, 2009

i used this code to make an example, and i traced some where of code. and i get two trace, i don' t get others, one of them is in FBSessionWaiting Handlers; FacebookEvent "YOU R LOGGED INTO FACEBOOK" and the other one is FBLogin funciton; "fb login"

i dont get any message from isAuthenticated, handleGetFriendsComplete, and also i can not iterate friendsDAta.friends etc...

code doesnt give me any error, but nothing happens. only traces are on my monitor.

i also added an circle shape while FBSession Waiting Handler, i dont see my shape there.

well do i do something wrong? thank you.

Posted By: sytr on September 7, 2009

...... i can not iterate friendsDAta.friends etc... i mean, it doenst trace facebook user name ...

Posted By: sytr on September 7, 2009

...... i can not iterate friendsDAta.friends etc... i mean, it doenst trace facebook user name ...

Posted By: pablodavi on February 8, 2010

friendID is String value and not Number like you put in your code, here in my facebook library. When i tryed get the first_name using GetFriends() the result is null. var friendID:Number = friendsData.friends.getItemAt(i).uid;

Some idea abou it ? Thanks and waiting. :)

Posted By: chrisaiv on February 10, 2010

pablodavi, You may need to cast your String object into a Number object. The easiest way to do that is simply wrap your code like this: Number( friendsData.friends.getItemAt(i).uid ).

Posted By: JamesStar on February 23, 2010

Hi there, is it possible to connect to a users data if they are not logged into Facebook?

James Star from Team Hi

Posted By: drKrueger on September 11, 2010

1.Does the FacebookSessionUtil check if you are already logged into Facebook? I want to test if a person is already logged into Facebook and then by-pass the login and extra browser; or is there something additional required that is tied to the application API ID?

  1. Why is it necessary to have an alert from Flash to confirm the user has logged into Facebook? Is this a timing issue?

Thanks

Posted By: msasays on April 28, 2011

thanks

You need to login to post a comment.