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.
我正在尝试编写一个使用flickr的api的简单网页。首先,我使用“flickr.places.find”来获取json文件。然后我获取json文件中的第一个对象,并使用其“woeid”和“place_id”尝试使用“flickr.photos.search”方法获取一个json文件,其中包含有关该地点照片的信息。我的“flickr.places.find”getJSON调用工作正常,但“flickr.photos.search”调用不起作用。
Here is my code (removed username and api_key for the post):
这是我的代码(删除了用户名和帖子的api_key):
<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 /*, ¶meter1=val1, ¶meter2=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):
对于这个地方,我进入“非洲”并单击按钮执行flickr api调用。第一个要执行的url就是这个(再次,删除了API_KEY和USER_ID,因此它不起作用):
First getJSON (flickr.places.find):
首先是getJSON(flickr.places.find):
My api call:
我的api电话:
which returns exactly what the flickr explorer returns:
它返回flickr资源管理器返回的内容:
Second getJSON (flickr.photos.search):
第二个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
Flickr的探险家返回此: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:
但是我的网址:
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.
我一直盯着这几个小时,但我没有看到这个问题。
1 个解决方案
#1
0
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.
我设法确定了问题。在构建api url时,我还附加了我的user_id,flickr.photos.serach函数认为它应该用作参数。这会导致getJSON调用尝试获取我在非洲拍摄的每张照片,这总共为0,导致数组为空。
#1
0
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.
我设法确定了问题。在构建api url时,我还附加了我的user_id,flickr.photos.serach函数认为它应该用作参数。这会导致getJSON调用尝试获取我在非洲拍摄的每张照片,这总共为0,导致数组为空。