Simple Facebook Using GraphAPI_Web.Swc In As3


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

a handy class to manage facebook connections using as3


Copy this code and paste it in your HTML
  1. package
  2. {
  3. import com.adobe.images.JPGEncoder;
  4. import com.facebook.graph.Facebook;
  5. import com.facebook.graph.data.FacebookAuthResponse;
  6. import flash.display.Bitmap;
  7. import flash.display.BitmapData;
  8. import flash.display.DisplayObject;
  9. import flash.events.Event;
  10. import flash.events.EventDispatcher;
  11. import flash.geom.Point;
  12. import flash.geom.Rectangle;
  13. import flash.net.URLLoader;
  14. import flash.net.URLLoaderDataFormat;
  15. import flash.net.URLRequest;
  16. import flash.system.Security;
  17. import flash.utils.ByteArray;
  18.  
  19. public class FacebookGraphAPI extends EventDispatcher
  20. {
  21. private var _permissions : Array;
  22. private var _appID : String ;
  23. private var _appSecret : String ;
  24. private var _userAuthResponse : FacebookAuthResponse = null;
  25. private var _apiSecuredPath:String = "https://graph.facebook.com";
  26. private var _apiUnsecuredPath:String = "http://graph.facebook.com";
  27. private var _userAuth : Boolean = false;
  28.  
  29. public function FacebookGraphAPI (appID : String, appSecret : String, permissions : Array)
  30. {
  31. _appID = appID;
  32. _appSecret = appSecret;
  33. _permissions = permissions;
  34. Security.loadPolicyFile(_apiSecuredPath + "/crossdomain.xml");
  35. Security.loadPolicyFile(_apiUnsecuredPath + "/crossdomain.xml");
  36. Facebook.init(_appID,handleIinit)
  37. }
  38. private function handleIinit(response:Object, fail:Object):void
  39. {
  40. SimpleFacebook.log.text ="iniciando";
  41. if (response != null)
  42. {
  43. Facebook.api("me/permissions",OnCompleteRequestPermissions);
  44. }
  45. else
  46. {
  47. dispatchEvent(new FacebookGraphAPIEvent(FacebookGraphAPIEvent.FINISHED_INIT));
  48. }
  49. }
  50. // check if the user has granted all the nedded permissions
  51. private function OnCompleteRequestPermissions(response : Object, fail : Object):void
  52. {
  53. var askForAllPermissions :Boolean = false
  54. for each (var perm : String in _permissions)
  55. {
  56. if (response[0][perm] == "undefined" || response[0][perm] == null)
  57. {
  58. askForAllPermissions = true;
  59. break;
  60. }
  61. }
  62. if (askForAllPermissions)
  63. {
  64. dispatchEvent(new FacebookGraphAPIEvent(FacebookGraphAPIEvent.FINISHED_INIT));
  65. }
  66. else
  67. {
  68. dispatchEvent(new FacebookGraphAPIEvent(FacebookGraphAPIEvent.FINISHED_INIT));
  69. AuthorizedUser(Facebook.getAuthResponse() as FacebookAuthResponse);
  70. }
  71.  
  72. }
  73. private function AuthorizedUser (response : FacebookAuthResponse) : void
  74. {
  75. _userAuthResponse = response;
  76. }
  77.  
  78. public function Login(e : Event = null) : void
  79. {
  80. if (_userAuthResponse == null)
  81. {
  82. Facebook.login(handleLogin, {scope: _permissions.toString()});
  83. }
  84. else
  85. {
  86. dispatchEvent(new FacebookGraphAPIEvent(FacebookGraphAPIEvent.LOGGED_IN));
  87. }
  88. }
  89. private function handleLogin(response:Object, fail:Object = null):void
  90. {
  91. if (Facebook.getAuthResponse().uid != null)
  92. {
  93. dispatchEvent(new FacebookGraphAPIEvent(FacebookGraphAPIEvent.LOGGED_IN));
  94. AuthorizedUser(Facebook.getAuthResponse() as FacebookAuthResponse);
  95. }
  96.  
  97. }
  98. public function PostScore(score : Number, callback : Function) : void
  99. {
  100. var data:Object = new Object();
  101. data.score = score;
  102. data.client_id = _appID;
  103. data.client_secret =_appSecret;
  104. Facebook.api(_userAuthResponse.uid+"/scores/",callback,data,"POST");
  105. }
  106.  
  107. public function FetchScores(callback : Function) : void
  108. {
  109. Facebook.api(_appID+"/scores/",callback);
  110. /*
  111. function callback(response : Object)
  112. for each(var object : Object in response)
  113. {
  114. object.score;
  115. object.user.name;
  116. object.user.id;
  117. }
  118. */
  119. }
  120. public function FetchUserImage(userID : String) : URLLoader
  121. {
  122. var pictureUrl: String = Facebook.getImageUrl(userID);
  123. var loader:URLLoader = new URLLoader();
  124. loader.dataFormat = URLLoaderDataFormat.BINARY;
  125. var request:URLRequest = new URLRequest(pictureUrl);
  126. loader.load(request);
  127. return loader;
  128. /*
  129. FetchUserImage(userID).addEventListener(Event.COMPLETE,EventListener);
  130. function EventListener (e:Event)
  131. {
  132. var loader:Loader = new Loader();
  133. loader.loadBytes(e.currentTarget.data);
  134. addChild(loader);
  135.   }
  136. */
  137. }
  138. public function PostImageToAlbum(message : String,image : DisplayObject,clipRect : Rectangle ) : void
  139. {
  140. var bd:BitmapData = new BitmapData(clipRect.width,clipRect.height,true,00000000);
  141. var AuxBData : BitmapData = new BitmapData(image.width,image.height,true,0);
  142. AuxBData.draw(image,null,null,null,clipRect);
  143. bd.copyPixels(AuxBData,clipRect,new Point(0,0));
  144. var result:Bitmap = new Bitmap(bd);
  145. var params: Object = new Object;
  146. var encoder:JPGEncoder = new JPGEncoder(75);
  147. var bytes:ByteArray = encoder.encode(result.bitmapData);
  148. params.message = message;
  149. params.image = bytes;
  150. params.fileName = "image.jpg";
  151. Facebook.api("/me/photos",null,params,"POST");
  152. }
  153. public function PostToUserWall(userId : String,message : String, name :String,caption : String, description : String, link : String) : void
  154. {
  155. var params: Object = new Object;
  156. params.message = message;
  157. params.name = name;
  158. params.caption = caption;
  159. params.description = description;
  160. params.link = link;
  161. Facebook.api("/"+userId+"/feed",null,params,"POST");
  162. }
  163. }
  164. }
  165.  
  166.  
  167. //////////////////////////////////EVENTS CLASS/////////////////////////////////
  168.  
  169.  
  170. package
  171. {
  172. import flash.events.Event;
  173.  
  174. public class FacebookGraphAPIEvent extends Event
  175. {
  176. public static const FINISHED_INIT:String =
  177. "FacebookGraphAPIEventFINISH_INIT";
  178. public static const LOGGED_IN:String =
  179. "FacebookGraphAPIEventLOGGED_IN";
  180. public var error : String = null
  181. public var data : Object = null;
  182. public function FacebookGraphAPIEvent(type : String,data : Object = null, error : String = null)
  183. {
  184. super(type);
  185. this.error = error;
  186. this.data = data
  187. }
  188.  
  189. }
  190. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.