我正在使用Force.com Apex和VF试图利用Cloudinary进行一些内容管理。我卡上的Chrome以下(不知道为什么会出现“未定义”作为云名称被JS定义):Cloudinary jQuery上传授权
POST https://api.cloudinary.com/v1_1/undefined/upload 401 (Unauthorized) ...... api.cloudinary.com/v1_1/undefined/upload:1
在无奈的点会很感激的任何援助。下面的代码(附注注释):
- 的APEX
:
public String getCldSig() {
Datetime d = datetime.now();
Long uxtime = d.getTime()/1000; //method provides epoch/unix time
String apisec = '<some_secret>';
String serial = 'callback=<some_url>&public_id=<some_id>×tamp=' + uxtime + apisec;
Blob sha = Crypto.generateDigest('SHA1', Blob.valueOf(serial));
String sig = EncodingUtil.convertToHex(sha); //perhaps I need to do UTF-8
String jsoSerial = '{"public_id":"<some_Id>",';
jsoSerial += '"timestamp":"'+ uxtime + '",';
jsoSerial += '"callback":"<some_url>",'; //maybe an issue with hosting the CORS html on a VF page.
jsoSerial += '"signature":"' + sig + '",';
jsoSerial += '"api_key":"<some_key>"}';
return jsoSerial.escapeHtml3(); //seems to be the right escape output HTML
}
的JavaScript/jQuery的:
$.cloudinary.config({"api_key":"<some_key>", "cloud_name":"<some_id>"}); $('.cloudinary-fileupload') .fileupload({ dropZone: ".sceneUpBtn", progress: function (e, data) { $(".progress").text("Uploading... " + Math.round((data.loaded * 100.0)/data.total) + "%"); } }); $('.cloudinary-fileupload').bind('fileuploadstart', function(e){ $('.sceneUpPrev').html('Upload started...'); }); $('.cloudinary-fileupload').bind('fileuploadfail', function(e){ $('.sceneUpPrev').html($.cloudinary.error); //**due to lack of documentation don't know how to get any specific error message using the jQuery library. Would expect messages similar to AWS S3 }); $('.cloudinary-fileupload').bind('cloudinarydone', function(e, data) { $('.sceneUpPrev').html( $.cloudinary.image(data.result.public_id, { format: data.result.format, version: data.result.version, crop: 'scale', width: 200 })); $('.image_public_id').val(data.result.public_id); return true; });
输入HTML:
<input class="cloudinary-fileupload"
data-cloudinary-field="upref"
data-form-data=""public_id":"<some_id>","timestamp":"1372282433","callback":"<some_url>","signature":"<some_sig>","api_key":"<some_key>"}"
id="sceneUpload"
name="file"
type="file">
你能解释一下你的第二点吗?你有没有尝试动态填充JSON参数,或者你是否在页面加载中实例化它们? – IvanR
我最初的错误是将JSON参数直接填充到INPUT的“数据表单数据”属性中。在设置“data-form-data”之前,FileUpload小部件正在进行实例化,结果认证失败,因为“data-form-data”在传递给Cloudinary时是空白的。 要解决这个问题,您不应该填充“data-form-data”,而应该在加载FileUpload小部件时填充“formData”参数(相同的区别)。 –
谢谢,我直接更新data-form-data属性时也犯了同样的错误。我只想为那些只在栈溢出处寻找答案并且不读文档的懒惰程序员添加唯一的东西 - 'fileupload'函数中的'formData'参数需要JSON哈希(或JavaScript对象)和** NOT * *一个序列化的JSON字符串 – IvanR