简体   繁体   中英

Why am I getting an 'Invalid Signature' error on Cloudinary Java?

I am trying to implement Cloudinary upload in a JSF application. As per instructions on Cloudinary's website, I am using this dependency:

<dependency>
    <groupId>com.cloudinary</groupId>
    <artifactId>cloudinary-http44</artifactId>
    <version>1.19.0</version>
</dependency>

I have a class that I am using for upload:

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");
    }
}

But when I try it, the Exception below is thrown:

Invalid Signature 6e527a754f1f6fd84df0bd4c092df881c0ddc65f. String to sign - 'timestamp=1533653472'.

Any help would be very much appreciated. Thanks.

I will recommend checking your upload to Cloudinary with a simple server-side java app. Something like this:-

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);
}

}

Once the above example is working you can move on testing the byte upload.This will ensure you don't have config issues.

Here is an example. I am using apache file upload:

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));
}

I've managed to get it working. I ended up copying the uploaded file contents from PrimeFaces uploadedFile into a temporary file and sending that file to Cloudinary upload.

Managed bean class method (upload handler):

public String upload() {
        try {
            File uploadedFile = File.createTempFile("image", ".tmp");

            InputStream content = file.getInputstream();

            Files.copy(content, uploadedFile.toPath(), StandardCopyOption.REPLACE_EXISTING);

            String filename = CloudinaryFacade.upload(uploadedFile);

            return Redirector.redirectWithMessage(filename, FacesMessage.SEVERITY_INFO, null);
        } catch (IOException ex) {
            return Redirector.redirectWithMessage(ex.getMessage(), FacesMessage.SEVERITY_ERROR, null);
        }
    }

Cloudinary upload method:

public static String upload(File file) throws IOException {
        Cloudinary cloudinary = new Cloudinary(CONFIG);
        Map<Object, Object> parameters = new HashMap<>();
        parameters.put("public_id", "Bookstore/Authors/Images/vejder");

        Map result = cloudinary.uploader().upload(file, parameters);

        return (String) result.get("url");
    }

Thank you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM