Search code examples
javascriptjqueryjsonrestflickr

flickr.photos.search works on flickr's explorer but not in my getJSON call


I'm trying to write a simple webpage that uses flickr's api. First I use "flickr.places.find" to get a json file. I then get the first object in the json file, and use its "woeid" and "place_id" to try to use the "flickr.photos.search" method to get a json file with the information pertaining to the place's photos. My "flickr.places.find" getJSON call works fine, but the "flickr.photos.search" call doesn't work.

Here is my code (removed username and api_key for the post):

<html>  
<head>  
<link href="bootstrap-3.3.6-dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<style>
</style>  

<!--flickr API Script-->
<script>  
    //API key
    var api_key = "API_KEY";
    //User ID
    var user_id = "USER_ID";

    /*Given a flikr api method, and any number of arguments in the format of:
    argumentName=value, return the url to be called using getJSON. This url will not
    call a callback function, and will return a JSON file*/
    function makeAPIURL(method /*, &parameter1=val1, &parameter2=val2, ...*/) {
        var api_url = "https://api.flickr.com/services/rest/?" + "&method=" + method;

        /*Append any arguments=value*/
        for(var i = 1; i < arguments.length; i++) {
            api_url += "&" + arguments[i];
        }

        api_url += "&api_key=" + api_key + 
        "&user_id=" + user_id +
        "&format=json" +
        "&nojsoncallback=1";

        return api_url;
    }



    function queryflickr() {

        var placeToFind = $("#placeToFind").val();
        /*Create api url to get the location info from flickr*/
        var api_url = makeAPIURL("flickr.places.find", "query=" + placeToFind);

        console.log("apiurl:" + api_url);

        /*Retrieves the JSON*/
        $.getJSON(api_url, function(json) {
            console.log(json.places);
            /*At this point we have the JSON file with places that are related to the queried string*/

            var placeFound = json.places.place[0]; 

            /*Tell the user what was found*/
            $("#retStatement").text('Found location "' + placeFound._content + '" which is type ' + placeFound.place_type);

            /*Get photos of the place from flickr*/
            /*Given the photos.search method, world ID, place ID*/
            api_url = makeAPIURL("flickr.photos.search", "woe_id=" + placeFound.woeid, "place_id=" + placeFound.place_id);

            console.log("Querying flickr for place's photos using apiurl:" + api_url);
            /*Retrieve the JSON*/
            $.getJSON(api_url, function(json) {
                console.log(json);
            });
        })
    }
</script>
</head>  

<body>  
<input type="text" placeholder="continent, country, region, locality, or neighborhood" class="form-control" id="placeToFind">

<center>
    <button class="btn btn-danger" id="button" onclick="queryflickr()">Populate!</button>
    <p id="retStatement"></p>
</center>


</body>  
</html>  

For the place, I enter in "Africa" and click the button to execute the flickr api calls. The first url to execute is this (again, removed the API_KEY and USER_ID so it won't work):

First getJSON (flickr.places.find):

My api call:

https://api.flickr.com/services/rest/?&method=flickr.places.find&query=Africa&api_key=[API_KEY]&user_id=[USER_ID]&format=json&nojsoncallback=1

which returns exactly what the flickr explorer returns:

https://api.flickr.com/services/rest/?method=flickr.places.find&api_key=2f396eba26d0c79c40a2e6d2390803b6&query=Africa&format=json&nojsoncallback=1&auth_token=72157671958290895-57134d5ebb08a350&api_sig=3ef0b79ef48b570e23bb5f203126049a

Second getJSON (flickr.photos.search):

The flickr explorer returns this: https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=2f396eba26d0c79c40a2e6d2390803b6&woe_id=24865670&place_id=6roy4axTVrJPAFMpHQ&format=json&nojsoncallback=1&auth_token=72157671958290895-57134d5ebb08a350&api_sig=a3925a01ef0e8eff9126f8619efef434

but my url:

https://api.flickr.com/services/rest/?&method=flickr.photos.search&woe_id=24865670&place_id=6roy4axTVrJPAFMpHQ&api_key=[API_KEY]&user_id=[USER_ID]&format=json&nojsoncallback=1

returns this:

{"photos":{"page":1,"pages":0,"perpage":250,"total":"0","photo":[]},"stat":"ok"}

No error, just an empty array. I used the same two arguments as the explorer as well.

I've been staring at this for a couple of hours but I don't see the issue.


Solution

  • I managed to determine the problem. When the api url is being constructed, I also append my user_id which the flickr.photos.serach function thinks it should use as an argument. This causes the getJSON call to try and get every photo that I'VE taken in Africa, which is a total of 0 causing the array to be empty.