1
votes

I am trying to upload my pictures to Cloudinary using their REST API. The Cloudinary library from CN1 worked very well for me, but I am looking to control a few things like FailSilently and being able to addToQueue and addToQueueAndWait.

This is my code, where I take a picture using the camera and then I upload to Cloudinary, but it doesn't seem to work:

@Override
protected void onMain_ButtonSubirAPIAction(Component c, ActionEvent event) {
    String picture = Capture.capturePhoto(width, -1);
    if(picture!=null){
        String cloudinaryURL = "https://api.cloudinary.com/v1_1/"+CLOUDNAME+"/image/upload";
        MultipartRequest request = new MultipartRequest() {
           protected void readResponse(InputStream input) throws IOException  {
              JSONParser jp = new JSONParser();
              Map <String, Object> result = jp.parseJSON(new InputStreamReader(input, "UTF-8"));
              labelX.setText((String)result.get("url"));
           }
        };

        request.setPost(true);
        request.setHttpMethod("POST");
        String encoded = Base64.encodeNoNewline((APIKEY + ":" + APISECRET).getBytes());
        request.addRequestHeader("Authorization", "Basic " + encoded);
        request.setUrl(cloudinaryURL);
        try {
            request.addData("file", picture, "image/jpeg");
            request.addArgument("public_id", "name1");
            NetworkManager.getInstance().addToQueue(request);
        } catch(Exception err) {
        }
    }
}

I have been trying to modify parameters indicated from their API documentation page: http://cloudinary.com/documentation/admin_api#usage_examples

Thanks

EDIT: Here is the syntax:

enter image description here

2

2 Answers

2
votes

There are some changes you need to do in your code. Please see this section.

The api_key and api_secret shouldn't be added as APIKEY + ":" + APISECRET like you did. This syntax is used only when using the Admin API, not Upload API.

At the request parameters:

  • You should include the api_key parameter set to your api_key.
  • You should include the timestamp parameter set to the current time.
  • This timestamp, along with the public_id and your api_secret should be signed. This should be the value of the signature parameter.
0
votes

The "@" notation is used for browsers and shouldn't be used for request. You need to use basic authentication as such:

String encoded = Base64.encodeNoNewline((APIKEY + ":" + APISECRET).getBytes());
request.addRequestHeader("Authorization", "Basic " + encoded);