我试图用flickr的api编写一个简单的网页。首先,我使用"flickr.places.find“来获取json文件。然后,我在json文件中获取第一个对象,并使用它的"woeid“和"place_id”尝试使用"flickr.photos.search“方法获得包含与该位置照片有关的信息的json文件。我的"flickr.places.find“getJSON调用工作正常,但"flickr.photos.search”调用不起作用。
下面是我的代码(删除用户名和帖子的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> 对于这个地方,我输入"Africa“并单击按钮执行flickr调用。执行的第一个url是这样的(再次删除了API_KEY和USER_ID,这样它就不能运行):
First getJSON (flickr.places.find):
我的api调用:
ID]&format=json&nojsoncallback=1
它完全返回flickr资源管理器返回的内容:
sig=3ef0b79ef48b570e23bb5f203126049a
Second getJSON (flickr.photos.search):
flickr资源管理器返回以下内容:sig=a3925a01ef0e8eff9126f8619efef434
但我的网址:
ID]&format=json&nojsoncallback=1
返回以下内容:
{“照片”:{“页”:1,“页”:0,“每页”:250,“总计”:“0”,“照片”:[]},"stat":"ok"}
没有错误,只有一个空数组。我也使用了与浏览器相同的两个参数。
我已经盯着这个看了几个小时了,但我不觉得有什么问题。
发布于 2016-08-04 18:48:42
我设法解决了这个问题。在构建api url时,我还附加了我的user_id,flickr.photos.serach函数认为它应该用作参数。这导致getJSON调用尝试获取在非洲拍摄的每一张照片,总共为0,导致数组为空。
https://stackoverflow.com/questions/38754256
复制相似问题