【问题标题】:Invalid signature when trying to upload to Cloudinary尝试上传到 Cloudinary 时签名无效
【发布时间】:2015-11-01 00:39:54
【问题描述】:

使用cloudinary_npm 提供的节点集成,当我尝试上传时收到以下消息:

{ error: { message: 'Invalid Signature t7233823748278473838erfndsjy8234. String to sign - \'timestamp=1439054775\'.', http_code: 401 } }

我检索然后将我的图像传递给后端,如下所示:

$scope.previewFile = function() {
   var file  = document.querySelector('input[type=file]').files[0];
   var reader  = new FileReader();

   if (file) {
     reader.readAsDataURL(file);
   } else {
     preview.src = "";
   }

   reader.onloadend = function () {
     base64img = reader.result;
     preview.src = base64img;
     console.log(base64img);
   };
};

 $scope.submitPic = function() {
    $http.post('http://localhost:3000/story/pic', {img: base64img})
    .success(function(data){
      preview.src = "";
    })
    .error(function(err){
      console.log(err);
    });
  };

然后在后面,我有以下配置和路线,都直接来自文档:

var cloudinary = require("cloudinary");
var CLOUD_API_SECRET = require("../constants.js");

cloudinary.config({
  cloud_name: 'some_cloud',
  api_key: '63789865675995',
  api_secret: CLOUD_API_SECRET
});

router.post('/pic', function(req, res, next) {
  var img = req.body.img;
  cloudinary.uploader.upload(img, function(result) {
  });

  res.status(200).send('ok');
});

有人知道我做错了什么吗?我已经解决了几个小时。我走到了尽头。

【问题讨论】:

  • submitPic,你确定base64img 有值吗? (为什么要发送 base64 而不是文件?)
  • 是的,当它到达后端时,我可以看到它在终端中以 base64 的形式出现,我忘了编辑它,但我确实继续,后来又将它转换为二进制所以我可以将其保存为 jpg 格式的文件。除此之外,我仍然不知道如何连接到 cloudinary。至于我为什么要发送base64,好吧,因为我不知道任何其他方式,老实说。使用 html5 文件阅读器,这就是我在文档中可以找到的所有内容,这将允许我将生成的文件输入用作数据 uri,这是我预览所需的。
  • 很可能require("../constants.js") 不返回字符串,因此CLOUD_API_SECRET 设置不正确。为了使该行工作,constants.js 应该有一行,形式为:module.exports = "my_secret";
  • 请在另一个 SO 问题中查看我的回答 - stackoverflow.com/questions/31897661/…

标签: upload signature cloudinary


【解决方案1】:

从 Java 级别开始,我通过将时区更改为 America/New_York 时间解决了这个问题:

Long time = new Long(System.currentTimeMillis() );
SimpleDateFormat sdf = new SimpleDateFormat();
sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"));

Date date = new Date(sdf.format(new Date(time)));
long utcDateInMilliSeconds = date.getTime();
params.put("timestamp", new Long(utcDateInMilliSeconds/1000));

【讨论】:

    【解决方案2】:

    确保您已将您的云端秘密放在 ''(引号/反逗号)中。确保结果语句应表示:

    var CLOUD_API_SECRET ='some_cloudinary_secret_xxx';

    在您从中获取此值的 js 文件中检查此值。

    【讨论】:

      【解决方案3】:

      我在使用 cloudinary 的 sdk 在 nodejs 上运行类似的代码路由时遇到了同样的错误。 这个问题原来是我的 API_SECRET 中的一个错字。

      【讨论】:

        【解决方案4】:

        就像 Jeremy 所说,这主要是您的 API 密码中的拼写错误或空白。

        尝试直接在配置中使用您的 API 密码(而不是通过变量)

        【讨论】:

          猜你喜欢
          • 2021-11-07
          • 2018-05-20
          • 2019-10-16
          • 2017-05-10
          • 1970-01-01
          • 2019-05-25
          • 2015-10-22
          • 2020-04-18
          • 2019-12-14
          相关资源
          最近更新 更多