/** * Upload Images to Facebook using Graph API. * MultipartURLLoader by Eugene Zatepyakin can be found here: http://bit.ly/9wx4q7 * * Send a bytearray, filename and the image's title. * var imageAttachment:Object = {byteArray: cFileReference.data, fileName: "image.jpg", imageTitle: "my image", message: "hello world"} * * optional: If you dont know the ID for the album to publish in, or you don't want to use facebook's default image location (app name) * var album:Object = {title: "My test album", description: "this is my album description"} * * */ private var albumProperties:Object; private var imageAttachment:Object; /** * Publish bytearray to users facebook photos. * * @param path | E.g "me/photos" or leave blank if you intend to upload to specified album. * @param imageAttachment | {byteArray:ByteArray, fileName:String, imageTitle:String, message:String} * @param albumProperties | {title:String , description:String } */ public function uploadImage(path:String, imageAttachment:Object, albumProperties:Object=null):void { this.imageAttachment = imageAttachment; this.albumProperties = albumProperties; if (albumProperties != null) { var loader:URLLoader = call("me/albums"); loader.addEventListener(FacebookOAuthGraphEvent.DATA, checkAlbumID); } else { uploadImageCall(path); } } /** * Checks if album title exists already (case sensative). * if it does it upload bytearray to it, otherwise it will create a new album. * * @param event */ private function checkAlbumID(event:FacebookOAuthGraphEvent):void { var jsobject:Object = (JSON.decode(event.rawData as String)); var arr:Array = jsobject.data as Array; var arrLength:int = arr.length-1; var createNewAlbum:Boolean = true; for(var i:int = arrLength; i>=0; i--) { if(String(arr[i].name) == albumProperties.title) { var path:String = String(arr[i].id)+"/photos"; uploadImageCall(path); createNewAlbum=false; } } if(createNewAlbum) createAlbum(); } /** * sends request to Graph API to create a new album. */ private function createAlbum():void { var data:URLVariables = new URLVariables(); data.name = albumProperties.title; data.message = albumProperties.description; var method:String = URLRequestMethod.POST; var loader:URLLoader = call("me/albums", data, method); loader.addEventListener(FacebookOAuthGraphEvent.DATA, onAlbumCreated); } /** * when album created dispatch album id to uploadImageCall * @param event */ private function onAlbumCreated(event:FacebookOAuthGraphEvent):void { var jsobject:Object = (JSON.decode(event.rawData as String)); var path:String = jsobject.id.toString() + "/photos"; uploadImageCall(path); } /** * Uploads bytearray to graph api. * * @param path - is either me/photos or albumid/photos */ private function uploadImageCall(path:String):void { var mpLoader:MultipartURLLoader = new MultipartURLLoader(); mpLoader.addVariable('message', imageAttachment.message); mpLoader.addFile(imageAttachment.byteArray, imageAttachment.fileName, imageAttachment.imageTitle); loaderAddListeners(mpLoader.loader); mpLoader.load(apiSecuredPath + '/'+ path + "&access_token="+ _token); }