首页 > 解决方案 > 为什么我在 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'.

任何帮助将不胜感激。谢谢。

标签: javacloudinary

解决方案


我建议使用简单的服务器端 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));
}

推荐阅读