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
+ "×tamp=" + 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