java - 为什么我在 Cloudinary Java 上收到“无效签名”错误?
问题描述
我正在尝试在 JSF 应用程序中实现 Cloudinary 上传。根据 Cloudinary 网站上的说明,我正在使用此依赖项:
<dependency>
<groupId>com.cloudinary</groupId>
<artifactId>cloudinary-http44</artifactId>
<version>1.19.0</version>
</dependency>
我有一个用于上传的课程:
package com.github.cvetan.bookstore.util;
import com.cloudinary.*;
import com.cloudinary.utils.ObjectUtils;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
*
* @author cvetan
*/
public class CloudinaryFacade {
private final static Map<Object, Object> CONFIG = new HashMap<>();
static {
CONFIG.put("cloud_name", "cvetan");
CONFIG.put("api_key", "***");
CONFIG.put("api_secret", "***");
}
public static String upload(byte[] file) throws IOException {
Cloudinary cloudinary = new Cloudinary(CONFIG);
Map result = cloudinary.uploader().upload(file, ObjectUtils.emptyMap());
return (String) result.get("url");
}
}
但是当我尝试它时,会抛出以下异常:
Invalid Signature 6e527a754f1f6fd84df0bd4c092df881c0ddc65f. String to sign - 'timestamp=1533653472'.
任何帮助将不胜感激。谢谢。
解决方案
我建议使用简单的服务器端 Java 应用程序检查您上传到 Cloudinary 的内容。像这样的东西: -
import com.cloudinary.*;
import com.cloudinary.utils.ObjectUtils;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class CloudinaryFacade {
private final static Map<Object, Object> CONFIG = new HashMap<>();
static {
CONFIG.put("cloud_name", "");
CONFIG.put("api_key", "");
CONFIG.put("api_secret", "");
}
public static void main(String[] args) throws IOException {
Cloudinary cloudinary = new Cloudinary(CONFIG);
Map result = cloudinary.uploader().upload("https://res.cloudinary.com/demo/image/upload/sample.jpg", ObjectUtils.emptyMap());
System.out.println(result);
}
}
一旦上面的示例运行,您就可以继续测试字节上传。这将确保您没有配置问题。
这是一个例子。我正在使用 apache 文件上传:
List<FileItem> formItems = upload.parseRequest(request);//List of file items
for (FileItem item : formItems) {
String fileName = item.getName();
//save on Cloudinary
Map imageUpload=cloudinary.uploader().upload(item.get(),
ObjectUtils.asMap("public_id",fileName));
}
推荐阅读
- r - R studio中ggplot()上x轴上的大数据
- python - 我很难创建一个找到tor节点的程序
- node.js - 文件上传并以可能的角度显示?
- google-apps-script - 通过脚本从电子表格中删除空行
- amazon-web-services - 长时间调用 aws api 网关
- ios - Swift - 带有英雄动画的 PanGestureRecognizer
- powershell - 如何在powershel脚本中将链接的.net调用分成多行?
- swift - 如何从 AVFoundationError 获取更多信息
- node.js - 快速网关获取“无法获取”
- java - 在 JavaFX 中使用 Google 地图和街景