我正在使用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