直接上传到Cloudinary从浏览器错误的请求 [英] Direct Upload to Cloudinary from Browser Bad Request

查看:361
本文介绍了直接上传到Cloudinary从浏览器错误的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图上传具有以下code到cloudinary时,得到一个400错误的请求错误:

i'm getting a 400 bad request error when trying to upload to cloudinary with the following code:

$("input.cloudinary-fileupload[type=file]").cloudinary_fileupload();
$.cloudinary.config({"api_key":"6586856745648955","cloud_name":"some-cloud"});

  $http.get("http://localhost:3000/story/secret")
  .then(function(res){
    var CLOUD_API_SECRET = res.data.CLOUD_API_SECRET;
    var obj =
    {
      "timestamp":  Date.now(),
      "callback": "http://localhost:3000/cloudinary_cors",
      "signature": CLOUD_API_SECRET,
      "api_key": "6586856745648955"
    };
    // var data = JSON.stringify(obj);
    $("input[type='file']").attr('data-form-data', obj);
  })
  .catch(function(err){
    console.log("error: ", err);
  });

我的面前包含以下内容:

with my front containing the following:

<input name="file" type="file"
    class="cloudinary-fileupload" data-cloudinary-field="image_upload"
    data-form-data=" ... html-escaped JSON data ... " >
</input>

我也试图字符串化,然后连接code中的目标文件的变量和插件,在像这样:

i've also tried to stringify, then encode the "obj" variable and plug that in like so:

var data = JSON.stringify(obj);
$("input[type='file']").attr('data-form-data', encodeURI(data));

我得到了同样的错误。

i get the same error.

我AP preciate任何帮助或建议。感谢一大堆。

i'd appreciate any help or suggestions. thanks a bunch.

推荐答案

在这里有几个问题:


  1. 您的帐户的 api_secret 不应该透露在您的客户端code。

  2. 签名不是 api_secret ,你应该生成一种基于签名的两个 api_secret 和上传选项。欲了解更多信息: http://cloudinary.com/documentation/upload_images#request_authentication
    签名生成必须在服务器端完成。

  3. Cloudinary预期在秒和毫秒不基于时间戳的签名,所以最后3位数是没有必要的。因此,我建议你来划分的时间戳1000省略最后3位数字(例如, Date.now()/ 1000

  1. Your account's api_secret should never be revealed in your client-side code.
  2. The signature isn't the api_secret, you should generate a signature which is based on both your api_secret and the upload options. For more information: http://cloudinary.com/documentation/upload_images#request_authentication. The signature generation must be done on a server-side.
  3. Cloudinary expects the signature to be based on the timestamp by seconds and not milliseconds, so the last 3 digits are not necessary. Therefore, I'd recommend you to divide the timestamp by 1000 to omit the last 3 digits (e.g., Date.now()/1000)

这篇关于直接上传到Cloudinary从浏览器错误的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆