Upload a picture to cloudinary using the REST API - actionscript-3

We are developing an App using AIR/AS3 and would like to upload a picture to cloudinary via the REST API from the client, instead of using Node.js.
Reading the Documentation of cloudinary, we have found that it should be done via a HTTP/S POST request (http://cloudinary.com/documentation/upload_images#remote_upload).
We've tried using a URLLoader and URLRequest passing the parameters as URLVariables.
Using this as a URL:
'https://api.cloudinary.com/v1_1/'+ CLOUD_NAME +'/raw/upload'
The code looks like this:
public function uploadImage(imageVO:CameraUIImageVO):void {
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, completeHandler, false, 0, true);
loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler, false, 0, true);
loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler, false, 0, true);
loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0, true);
var request:URLRequest = new URLRequest(_url);
request.data = getVariables(imageVO);
request.method = URLRequestMethod.POST;
loader.load(request);
}
private function getVariables(imageVO:CameraUIImageVO):URLVariables {
var variables:URLVariables = new URLVariables();
variables.timestamp = _timestamp;
var bitmapData:BitmapData = imageVO.image.bitmapData;
var rawBytes:ByteArray = bitmapData.encode(bitmapData.rect, new JPEGEncoderOptions(50));
variables.file = rawBytes;
variables.resource_type = 'raw';
variables.signature = getSignature("anyPublicId");
variables.api_key = API_KEY;
return variables;
}
private function getSignature(publicId:String):String {
var toHash:String =
"public_id="+ publicId
+ "&timestamp=" + timestamp
+ API_SECRET;
var src:ByteArray = Hex.toArray(Hex.fromString(toHash));
var sha1:SHA1 = new SHA1();
var hashedString:String = Hex.fromArray(sha1.hash( src ));
return hashedString;
}
private function get timestamp():String {
return _timestamp = Number(new Date().time).toString();
}
The result of this is a http error 401

It seems you're signing the public_id, however the public_id isn't passed in options of the upload itself. i.e., there's no:
variables.public_id = "anyPublicId";
inside the getVariables method.

Related

Cancel a POST request

I have an app that makes a request to a server to generate an audio file and the problem that I have is that if I send two distinct and consecutive requests, I can not cancel the first. I tried with .unload or .unloadAndStop but always gives me error.
Any suggestions?
elfich="sonido" + elfichnum;
var request:URLRequest = new URLRequest();
var variables:URLVariables = new URLVariables();
var loader:URLLoader = new URLLoader();
variables.text=caja_texto;
variables.fileName=elfich;
request.url = "http://192.168.0.19:800/cgi-bin/tts.cgi";
request.method = URLRequestMethod.POST;
request.data = variables;
loader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, cargacompletanew);
loader.addEventListener(IOErrorEvent.IO_ERROR, onErrornew);
loader.load(request);
}
function cargacompletanew(event:Event):void
{
xlocalSound = new Sound();
var xreq:URLRequest = new URLRequest("http://192.168.0.19:800/output/" + elfich + ".mp3");
xlocalSound.load(xreq);
xlocalSound.addEventListener(Event.OPEN, iniciarSonido);
}
What about
loader.removeEventListener(Event.COMPLETE, cargacompletanew);
loader.removeEventListener(IOErrorEvent.IO_ERROR, onErrornew);
loader.close();
I think there was a problem with close() if you were trying to close it if nothing was loaded, so I'd try to put that inn try/catch block.
try
{
loader.close();
}
catch(e:Error)
{
loader("Oh noes!");
}

ActionScript 3.0 web scraper works in flash, but not browser

so I have this issue. I recently made a flash webscraper to grab a video source link. It works in flash, however when I test in the browser I only get a blank canvas... It does not work. I have no idea as to what the problem is... I just started learning actionscript today, so I'm far from the greatest at it.
import flash.net.*
import flash.events.*;
import flash.display.*;
var theUrl = "http://www.sockshare.com/file/384F14398D224634";
firstStep();
function firstStep() {
var urlReq: URLRequest = new URLRequest(theUrl);
urlReq.method = URLRequestMethod.POST;
var loader: URLLoader = new URLLoader(urlReq);
loader.addEventListener(Event.COMPLETE, onSuccess);
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.load(urlReq);
function onSuccess(e: Event): void {
var theContents1: String = String(loader.data);
var pattern1: RegExp = /<input type="hidden" value="(?P<innertext>.*?)" name="hash">/;
var result1 = pattern1.exec(theContents1);
goToSecond(result1.innertext);
}
}
function goToSecond(hash: String) {
var urlReq2: URLRequest = new URLRequest(theUrl);
urlReq2.method = URLRequestMethod.POST;
var urlVars2: URLVariables = new URLVariables();
urlVars2.hash = hash;
urlVars2.confirm = 'Continue+as+Free+User';
urlReq2.data = urlVars2;
var loader2: URLLoader = new URLLoader(urlReq2);
loader2.addEventListener(Event.COMPLETE, onSuccess2);
loader2.dataFormat = URLLoaderDataFormat.VARIABLES;
loader2.load(urlReq2);
function onSuccess2(e: Event): void {
var theContents2: String = String(loader2.data);
var pattern: RegExp = /playlist%3A%20%27%2F(?P<innertext>.*?)%27%2C%0D%0A%09plugins/;
var result = pattern.exec(theContents2);
var linkp1: String = "http://www.sockshare.com/" + unescape(result.innertext);
finalStep(linkp1);
}
}
function finalStep(finalUrl: String) {
var urlReq3: URLRequest = new URLRequest(finalUrl);
urlReq3.method = URLRequestMethod.POST;
var loader3: URLLoader = new URLLoader(urlReq3);
loader3.addEventListener(Event.COMPLETE, onSuccess3);
loader3.dataFormat = URLLoaderDataFormat.TEXT;
loader3.load(urlReq3);
function onSuccess3(e: Event): void {
var theContents3: String = String(loader3.data);
var pattern2: RegExp = /<media:content url="(?P<innertext>.*?)" type="/;
var result2 = pattern2.exec(theContents3);
var finalLink: String = result2.innertext;
trace(finalLink);
mainText.text = finalLink;
}
}
It's sandbox problem, read here, if you'll publish as standalone (ex: Air) project it would work

Looking to use Firebase with Actionscript 3 / Air

I have found Firebase, and it looks excellent for javascript / HTML5 usage.
But I was wondering if there is also an Actionscript API?
E.g
var myRootRef = new Firebase('https://myprojectname.firebaseIO-demo.com/');
myRootRef.set('Hello World!');
var dataRef = new Firebase('https://SampleChat.firebaseIO-demo.com/users/fred/name/first');
dataRef.on('value', function(snapshot) {
alert('fred’s first name is ' + snapshot.val());
});
So to set data and have listeners for updated data etc.
Thanks for any help
Matt
Doesn't look like there is an AS3 api available but the good news is that they have a rest api which is cross platform that you can write an AS3 wrapper around. (https://www.firebase.com/docs/rest-api.html) That is what I plan to do. I Want to use it with GameBuilder Studio.
If you are planning on embedding your application inside a web page (that you create) you could use an external javascript interface and communicate to firebase through that.
I'm planning on doing this myself to see how it turns out.
Connecting to Firebase using ActionScript 3 only requires the use of URLRequest and URLLoader. The following examples cover the 4 basic operations (CRUD).
To read data from an specific node from your Firebase database:
private function loadNews():void
{
var request:URLRequest = new URLRequest("https://<YOUR-PROJECT-ID>.firebaseio.com/<Node_to_read>.json");
var loader:URLLoader = new URLLoader();
loader.addEventListener(flash.events.Event.COMPLETE, newsLoaded);
loader.load(request);
}
private function newsLoaded(event:flash.events.Event):void
{
trace(event.currentTarget.data);
var rawData:Object = JSON.parse(event.currentTarget.data);
}
To insert data to a specific node:
private function saveEntry(title:String, description:String):void
{
var myObject:Object = new Object();
myObject.title = title;
myObject.description = description;
myObject.timestamp = new Date().getTime();
var request:URLRequest = new URLRequest("https://<YOUR-PROJECT-ID>.firebaseio.com/<Node_to_insert>.json");
request.data = JSON.stringify(myObject);
request.method = URLRequestMethod.POST;
var loader:URLLoader = new URLLoader();
loader.addEventListener(flash.events.Event.COMPLETE, entrySent);
loader.load(request);
}
private function entrySent(event:flash.events.Event):void
{
trace(event.currentTarget.data);
}
To delete a specific node:
private function deleteEntry():void
{
var header:URLRequestHeader = new URLRequestHeader("X-HTTP-Method-Override", "DELETE");
var request:URLRequest = new URLRequest("https://<YOUR-PROJECT-ID>.firebaseio.com/<Node_to_delete>.json");
request.method = URLRequestMethod.POST;
request.requestHeaders.push(header);
var loader:URLLoader = new URLLoader();
loader.addEventListener(flash.events.Event.COMPLETE, entryDeleted);
loader.load(request);
}
private function entryDeleted(event:flash.events.Event):void
{
trace(event.currentTarget.data);
}
To update/modify data to a specific node:
private function updateEntry(title:String, description:String):void
{
var header:URLRequestHeader = new URLRequestHeader("X-HTTP-Method-Override", "PATCH");
var myObject:Object = new Object();
myObject.title = title;
myObject.description = description;
var request:URLRequest = new URLRequest("https://<YOUR-PROJECT-ID>.firebaseio.com/journal/<Node_to_modify>.json");
request.data = JSON.stringify(myObject);
request.method = URLRequestMethod.POST;
request.requestHeaders.push(header);
var loader:URLLoader = new URLLoader();
loader.addEventListener(flash.events.Event.COMPLETE, entryUpdated);
loader.load(request);
}
private function entryUpdated(event:flash.events.Event):void
{
trace(event.currentTarget.data);
}
If you want further information I have written detailed Firebase REST guides and examples on how to use ActionScript 3 and Firebase.

Imdb api with flash - as3 - flex

I am trying to use the imdb API, in this case: http://imdbapi.org/
I need to search for a movie by name and get a json, then load an image with the poster obtained.
I'll be using as3 - flex to generate an Air package.
I tried this example, but can't seem to get it right.
import flash.net.*;
var url:String = "http://imdbapi.org/";
var request:URLRequest = new URLRequest(url);
request.method = URLRequestMethod.GET;
var variables:URLVariables = new URLVariables();
variables.name = "Pulp fiction";
request.data = variables;
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onComplete);
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.load(request);
function onComplete (event:Event):void {
trace(event.target.data);
}
Perhaps you could enlight me with an example of connecting to the api and retrieving that json so that I can load an image with the poster and generate my air package.
Many thanks!
The API seems to want the movie in the q param so change this
variables.name = "Pulp fiction";
to :
variables.q = "Pulp fiction";
To verify : http://imdbapi.org/?q=Pulp%20Fiction
From there getting the poster URL is just a matter of reading the correct property from the JSON string.
private function onComplete (event:Event):void {
var data:Array = JSON.parse(event.target.data);
if(data && data.length)
{
var movie:Object = data[0];
trace(movie.poster);
}
}

AS3/AIR: IOStream Error when trying to upload files

I'm making an AIR app in which the user drags and drops image files onto the stage, the app then uploads those dropped images to the site imgur.com, and then returns with links to the images (in XML format).
I am having two issues:
the program works successfully with a very small file, however anything larger (1mb or more), I get an IOStream error 2023. Imgur supports uploads of up to 10mb so I know it must be something on my end.
I would like to be able to drop multiple image files at once into the program, and have it upload all of them (either synchronously or asynchronously, it does not matter). Right now the program gives errors when I attempt to do this (something along the lines of the byteArray having to be greater than 0).
Below is the code in question which is giving me issues. (thank you for your help in advanced)
private function doDragDrop(e:NativeDragEvent):void
{
trace("doDragDrop() called.");
var dropFiles:Array = e.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
for each (var file:File in dropFiles)
{
switch (file.extension.toLowerCase())
{
case "jpg" :
case "jpeg" :
case "gif" :
case "png" :
case "apng" :
case "tiff" :
case "bmp" :
case "pdf" :
case "xcf" :
trace("file Extension Check = passed");
addImage(file.nativePath);
break;
//error handling for non-supported filetypes
default :
trace("ERROR: Unsupported File Type Detected!");
}
}
}
private function addImage(nativePath:String):void
{
trace("addImage() called.");
trace("NativePath is: " + nativePath);
//var file:File = new File(nativePath);
var file:File = File.desktopDirectory.resolvePath(nativePath);
var ba:ByteArray = new ByteArray();
var stream:FileStream = new FileStream();
stream.open(file, FileMode.READ);
stream.readBytes(ba);
stream.close();
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, fileLoaded);
loader.loadBytes(ba);
}
private function fileLoaded(e:Event):void
{
trace("fileLoaded() called.");
var bitmap:Bitmap = Bitmap(e.target.content);
var bitmapData:BitmapData = bitmap.bitmapData;
var png:ByteArray = PNGEncoder.encode(bitmapData);
urlLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
urlLoader.addEventListener(Event.COMPLETE, onCookieSent);
urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
var vars:String = "?key=" + API_KEY + "&name=name&title=title";
var request:URLRequest = new URLRequest(UPLOAD_URL + vars);
request.contentType = "application/octet-stream";
request.method = URLRequestMethod.POST;
request.data = png;
urlLoader.load(request);
}
private function onCookieSent(e:Event):void
{
trace("onCookieSent() called.");
var res:XML = new XML(unescape(urlLoader.data));
var resultsList:XMLList = res.links;
trace(resultsList);
}
private function onIOError(e:IOErrorEvent):void
{
trace("ioErrorHandler: " + e);
TweenLite.to(tv_mc, 2, {alpha:0.5, scaleX:1.0, scaleY:1.0, ease:Elastic.easeOut});
// Handle error
}
private function onSecurityError(e:SecurityErrorEvent):void
{
trace("securityErrorHandler: " + e);
TweenLite.to(tv_mc, 2, {alpha:0.5, scaleX:1.0, scaleY:1.0, ease:Elastic.easeOut});
// handle error
}
//When the dragged object leaves the drop point, do this
private function doDragExit(e:NativeDragEvent):void
{
trace("doDragExit() called.");
TweenLite.to(tv_mc, 2, {alpha:0.5, scaleX:1.0, scaleY:1.0, ease:Elastic.easeOut});
}
after doing this:
var ba:ByteArray = new ByteArray();
var stream:FileStream = new FileStream();
stream.open(file, FileMode.READ);
stream.readBytes(ba);
stream.close();
you already have a ByteArray of the file/image. can't you just send this one...
private function addImage(nativePath:String):void
{
trace("addImage() called.");
trace("NativePath is: " + nativePath);
//var file:File = new File(nativePath);
var file:File = File.desktopDirectory.resolvePath(nativePath);
var ba:ByteArray = new ByteArray();
var stream:FileStream = new FileStream();
stream.open(file, FileMode.READ);
stream.readBytes(ba);
stream.close();
urlLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
urlLoader.addEventListener(Event.COMPLETE, onCookieSent);
urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
var vars:String = "?key=" + API_KEY + "&name=name&title=title";
var request:URLRequest = new URLRequest(UPLOAD_URL + vars);
request.contentType = "application/octet-stream";
request.method = URLRequestMethod.POST;
request.data = ba;
urlLoader.load(request);
}