使用REST API将图片上传到cloudinary

时间:2015-02-20 11:42:57

标签: actionscript-3 air cloudinary urlrequest as3crypto

我们正在使用AIR / AS3开发一个应用程序,并希望通过REST API从客户端上传图片到cloudinary,而不是使用Node.js。

阅读cloudinary的文档,我们发现它应该通过HTTP / S POST请求(http://cloudinary.com/documentation/upload_images#remote_upload)来完成。

我们尝试使用URLLoader和URLRequest将参数作为URLVariables传递。

将此作为网址使用: 'https://api.cloudinary.com/v1_1/'+ CLOUD_NAME +'/raw/upload'

代码如下所示:

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();
}

结果是http错误401

1 个答案:

答案 0 :(得分:1)

您似乎正在签署public_id,但public_id并未通过上传本身的选项。即,没有:

variables.public_id = "anyPublicId";

getVariables方法中。