Posted By

chrisaiv on 04/20/09


Tagged

Flex facebook


Versions (?)

Who likes this?

5 people have marked this snippet as a favorite

abbey
alptugan
zoo
swfgeek
dubogii


Flex: Get Photo Albums in Facebook


 / Published in: ActionScript 3
 

URL: http://www.adobe.com/devnet/facebook/samples/index.html#albumlist

This is pretty much Adobe's Sample #2 with a few nice improvements

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="initApp()">
  3. <mx:Script>
  4. <![CDATA[
  5. import mx.controls.Alert;
  6. import com.facebook.data.photos.GetAlbumsData;
  7. import com.facebook.commands.photos.GetAlbums;
  8. import com.facebook.net.FacebookCall;
  9. import com.facebook.Facebook;
  10. import com.facebook.events.FacebookEvent;
  11. import flash.display.Stage;
  12. import flash.display.LoaderInfo;
  13. import com.facebook.utils.FacebookSessionUtil;
  14. import mx.collections.ArrayCollection;
  15.  
  16. private var API_KEY:String = "API_KEY";
  17. private var SECRET_KEY:String = "SECRET_KEY";
  18.  
  19. private var fbook:Facebook;
  20. private var fbLoaderInfo:LoaderInfo;
  21. private var fbSession:FacebookSessionUtil;
  22.  
  23. [Bindable]
  24. private var fbPhotoAlbums:ArrayCollection;
  25.  
  26. private var currentMethod:Function;
  27.  
  28. private function initApp():void
  29. {
  30. fbLoaderInfo = stage.loaderInfo;
  31.  
  32. fbPhotoAlbums = new ArrayCollection();
  33. }
  34.  
  35. private function fbLogin():void
  36. {
  37. fbSession = new FacebookSessionUtil( API_KEY, SECRET_KEY, loaderInfo );
  38. fbSession.addEventListener( FacebookEvent.WAITING_FOR_LOGIN, fbWaitingForLoginHandler, false, 0, true );
  39. fbSession.addEventListener( FacebookEvent.CONNECT, fbOnConnectHandler, false, 0, true );
  40. fbook = fbSession.facebook;
  41. fbSession.login();
  42. }
  43.  
  44. private function updateStatus( message:String ):void
  45. {
  46. fbStatus.text = message;
  47. }
  48.  
  49. private function isAuthenticated( method:Function ):Boolean
  50. {
  51. //Store which method the current user is on
  52. currentMethod = method;
  53.  
  54. //If a FacebookSession has not been created or user is not authenticated, Force a Login()
  55. if( fbSession == null || !fbSession.facebook.is_connected ){
  56. fbLogin();
  57.  
  58. return false;
  59. }
  60. return true;
  61. }
  62.  
  63. /**************************************
  64. * Facebook POST Request
  65. **************************************/
  66. private function getPhotoAlbums():void
  67. {
  68. //Once Authentication has been solved, continue with the user's process
  69. if( isAuthenticated( arguments.callee ) ){
  70. var call:FacebookCall = fbook.post( new GetAlbums( fbook.uid ) );
  71. call.addEventListener( FacebookEvent.COMPLETE, photoAlbumsCompleteHandler, false, 0, true );
  72. }
  73. }
  74.  
  75. /**************************************
  76. * Facebook Event Handlers
  77. **************************************/
  78. private function fbOnConnectHandler( e:FacebookEvent ):void
  79. {
  80. updateStatus( "You are logged into Facebook" );
  81. //Continue where the user last left off before requiring Authentication
  82. currentMethod();
  83. }
  84.  
  85. private function fbWaitingForLoginHandler( e:FacebookEvent ):void
  86. {
  87. //The user has returned from the log-in page and are now clicking "OK"
  88. var alert:Alert = Alert.show( "Click OK after you've logged in", "Logging In" );
  89. alert.addEventListener( Event.CLOSE, alertCloseHandler );
  90. }
  91.  
  92.  
  93. private function fbValidateLogin( ):void
  94. {
  95. fbSession.validateLogin();
  96. }
  97.  
  98. private function alertCloseHandler( e:Event ):void
  99. {
  100. //Verify that the user has logged in
  101. fbValidateLogin();
  102. }
  103.  
  104. private function photoAlbumsCompleteHandler( e:FacebookEvent ):void
  105. {
  106. var albumsResponseData:GetAlbumsData = e.data as GetAlbumsData;
  107.  
  108. if( !albumsResponseData || e.error ){
  109. trace( "photoAlbumsCompleteHandler: " + e.error );
  110. updateStatus( "There was an error with collecting photo albums" );
  111. return
  112. }
  113.  
  114. //Iterate through the albumsResponseData.albumCollection to get the titles
  115. for( var i:uint = 0; i < albumsResponseData.albumCollection.length; i++ ){
  116. fbPhotoAlbums.addItem( albumsResponseData.albumCollection.getItemAt( i ) );
  117. }
  118.  
  119. }
  120.  
  121.  
  122. ]]>
  123. </mx:Script>
  124.  
  125. <mx:Button label="Get Photo Albums" click="getPhotoAlbums()" x="10" y="10"/>
  126.  
  127. <mx:Label text="Status" color="0xFFFF00" fontWeight="bold" fontSize="14" x="162" y="8"/>
  128. <mx:Text id="fbStatus" text="Please Log In" x="223" y="11" />
  129.  
  130. <mx:DataGrid dataProvider="{fbPhotoAlbums}" width="50%" y="72" x="10">
  131. <mx:columns>
  132. <mx:DataGridColumn headerText="Album Name" dataField="name" />
  133. <mx:DataGridColumn headerText="Date" dataField="created" />
  134. </mx:columns>
  135. </mx:DataGrid>
  136.  
  137. </mx:Application>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: roberton on May 20, 2010

hi, I try the sample "Flex: Get Photo Albums in Facebook" When I run it I can login, but when I "click" Get Photo Albums not display nothing just the DataGrid empty. Whit the Safari I look at activity and it show "http://api.facebook.com/restserver.php"

Can you tell me why or where is my problem?

Thanks,

Bob

Posted By: roberton on May 20, 2010

hi, I try the sample "Flex: Get Photo Albums in Facebook" When I run it I can login, but when I "click" Get Photo Albums not display nothing just the DataGrid empty. Whit the Safari I look at activity and it show "http://api.facebook.com/restserver.php"

Can you tell me why or where is my problem?

Thanks,

Bob

You need to login to post a comment.