Skip to main content

Read All Image URLs Using JQuery

Comments

3 comments

  • Roee Ben Ari

    Hi Graham, thanks for reaching out. 

    Regarding the error you see, did you make sure to replace the "cloudName" part of the request URL with your own cloud name (you can see it in https://cloudinary.com/console).

    For example, if your cloud name is "graham" then the URL should be:

    https://api.cloudinary.com/v1_1/graham/resources/image

    in addition, the request should be done using "GET" rather than "POST".

    Finally, the API uses basic auth so you should pass your credentials with something like this:

    beforeSend: function (xhr) {
    xhr.setRequestHeader ("Authorization", "Basic " + btoa(apiKey + ":" + secret));
    },

    The more important issue though is that you shouldn't perform such a request on the client side as it will expose your cloud's secret which is definitely something you should keep safe.
    As the API to retrieve resources data is part of the Cloudinary Admin API it would allow anyone who obtains your API key and secret to perform all actions on your account. This kind of operation should be done on your own server rather than on the client in order to protect your account.

    0
  • Graham Roy

    Hi Roee.

     

    Thanks for your reply. I did include the cloudname in the url and I have made the changes you suggested but it still doesn't work.

    Here is my function:

    function updatePhotoAlbum(){
      var url = "https://api.cloudinary.com/v1_1/cloudName/resources/image";
      $.ajax({
          type:"GET",
          beforeSend: function(request){
          var API_KEY = "apikey";
          var API_SECRET = "apisecret";
          request.setRequestHeader("Authorization","Basic" + btoa(API_KEY + ":" + API_SECRET));
        },
          url: url,
          success: function(response){
          console.log(response);
        }
      });
    }

    but I still get an error:

    Failed to load resource: the server responded with a status of 404 (Not Found)

     

    in the console. When I click the link it refers to go I go the resources json

    {"resources":[{"public_id":"arvponfkoibwhkz3jfao","format":"jpg","version":1508874121,"resource_type":"image","type":"upload","created_at":"2017-10-24T19:42:02Z","bytes":390795,"width":1280,"height":960,"url":"http://res.cloudinary.com/cloudName/image/upload/v1508874121/arvponfkoibwhkz3jfao.jpg","secure_url":"https://res.cloudinary.com/cloudName/image/upload/v1508874121/arvponfkoibwhkz3jfao.jpg"},{"public_id":"ydglvezbrsbxy8wtixy1","format":"jpg","version":1508873757,"resource_type":"image","type":"upload","created_at":"2017-10-24T19:35:57Z","bytes":1061637,"width":2592,"height":1936,"url":"http://res.cloudinary.com/cloudName/image/upload/v1508873757/ydglvezbrsbxy8wtixy1.jpg","secure_url":"https://res.cloudinary.com/cloudName/image/upload/v1508873757/ydglvezbrsbxy8wtixy1.jpg"},{"public_id":"sample","format":"jpg","version":1508534685,"resource_type":"image","type":"upload","created_at":"2017-10-20T21:24:45Z","bytes":109669,"width":864,"height":576,"url":"http://res.cloudinary.com/cloudName/image/upload/v1508534685/sample.jpg","secure_url":"https://res.cloudinary.com/dyjubu34g/image/upload/v1508534685/sample.jpg"},

     

    0
  • Roee Ben Ari

    Hi Graham,

    It looks like the request is blocked because our server doesn't allow cross-domain requests on the API for security reasons. 
    This ties to the second part of the answer given above. You shouldn't be doing this kind of request in the browser - as it also seems to be not only non-recommended but impossible.

    0

Post is closed for comments.