问题
I am trying to fetch all the post from a blog using the Blogger API. The maximum number of posts to fetch seems to be limited to 20 for some unknown reasons.
If I try this URL:
https://www.googleapis.com/blogger/v3/blogs/3089072491354463482/posts?maxResults=20&fields=items(title)&key=AIzaSyAJO5J-pRCaGOIeRLIJfvAPwxpMLKvwebU
I get the following response (listing the last 20 post titles as expected):
{
"items": [
{
"title": "El Caballero"
},
{
"title": "Une traversée de frontière… étonnante!"
},
{
"title": "Hasta luego querida Argentina!"
},
{
"title": "Dernier jour en Argentine"
},
{
"title": "Humahuaca"
},
{
"title": "Purmamarca"
},
{
"title": "Tilcara"
},
{
"title": "Premières grèves"
},
{
"title": "Le Nord Argentin: Salta"
},
{
"title": "Ca en fait de l'eau tout ça..."
},
{
"title": "Un peu de pluie au Brésil"
},
{
"title": "Iguazu"
},
{
"title": "San José"
},
{
"title": "Adieu à Buenos Aires"
},
{
"title": "Traversons en Uruguay"
},
{
"title": "Retour à Buenos Aires"
},
{
"title": "Fin de l'aventure Patagonienne"
},
{
"title": "Les fameuses tours nous surprennent"
},
{
"title": "Un peu de pluie pour se changer les idées"
},
{
"title": "Valle Francés"
}
]
}
However, if I increase the maxResults parameters,
https://www.googleapis.com/blogger/v3/blogs/3089072491354463482/posts?maxResults=21&fields=items(title)&key=AIzaSyAJO5J-pRCaGOIeRLIJfvAPwxpMLKvwebU
I get the following error:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "invalid",
"message": "Invalid Value"
}
],
"code": 400,
"message": "Invalid Value"
}
}
How could I increase the limitation in the maximum number of post that I can fetch?
Thanks,
Nicolas
回答1:
I think that the API is limited to only pull 20 results as a maximum.
So in order to fetch more than 20 results you have to use the pageToken
parameter as specified in Bloggers's reference API.
Your first request should include the nextPageToken
so you will have it in the response. Than use this token to retrieve the next page and so on.
Your first request must be like this :
https://www.googleapis.com/blogger/v3/blogs/3089072491354463482/posts?maxResults=20&fields=items%28title%29%2CnextPageToken&key=AIzaSyAJO5J-pRCaGOIeRLIJfvAPwxpMLKvwebU
Here is the expected result :
{
"nextPageToken": "CgkIChignPaz5ycQ-rn0pIfipe8q",
"items": [
{
"title": "El Caballero"
},
{
"title": "Une traversée de frontière… étonnante!"
},
{
"title": "Hasta luego querida Argentina!"
},
{
"title": "Dernier jour en Argentine"
},
{
"title": "Humahuaca"
},
{
"title": "Purmamarca"
},
{
"title": "Tilcara"
},
{
"title": "Premières grèves"
},
{
"title": "Le Nord Argentin: Salta"
},
{
"title": "Ca en fait de l'eau tout ça..."
}
]
}
Now all you have to do is to pick the "nextPageToken": "CgkIChignPaz5ycQ-rn0pIfipe8q"
in the result and include it in you next request like this :
https://www.googleapis.com/blogger/v3/blogs/3089072491354463482/posts?%20maxResults=20&pageToken=CgkIChignPaz5ycQ-rn0pIfipe8q&fields=items%28title%29%2CnextPageToken&key=AIzaSyAJO5J-pRCaGOIeRLIJfvAPwxpMLKvwebU
The result should show the next 20 posts in addition to a new ``nextPageToken` to use in the next request.
回答2:
I implemented what was suggested by Walid Laribi for a travel blog page that retrieve the location of all the posts from the blog and draw the path taken during the travel (blog).
To fetch the location of all the posts, I have first a script that retrieves the first 10 posts and gives me the nextPageToken
:
<script src="https://www.googleapis.com/blogger/v3/blogs/3089072491354463482/posts?fields=nextPageToken,items(title,location(name,lat,lng),url,published)&key=AIzaSyAJO5J-pRCaGOIeRLIJfvAPwxpMLKvwebU&callback=handleResponse">
In the callback of the previous script, we can get the location of each post as well as creating and executing the script to fetch the next posts:
// Get latitude/longitude from Blogger
function handleResponse(response) {
for(i=0; i< response.items.length; i++){
if(response.items[i].location != undefined){
Lat.push(response.items[i].location.lat);
Lng.push(response.items[i].location.lng);
// etc.
}
}
if(response.nextPageToken != undefined){
var srctxt = 'https://www.googleapis.com/blogger/v3/blogs/3089072491354463482/posts?fields=nextPageToken,items(title,location(name,lat,lng),url,published)&key=AIzaSyAJO5J-pRCaGOIeRLIJfvAPwxpMLKvwebU&callback=handleResponse&pageToken=' + response.nextPageToken;
// Execute the new script
var head = document.getElementsByTagName('head')[0];
var scriptElement = document.createElement('script');
scriptElement.setAttribute('type', 'text/javascript');
scriptElement.setAttribute('src', srctxt);
head.appendChild(scriptElement);
head.removeChild(scriptElement);
}
}
来源:https://stackoverflow.com/questions/17184238/limitation-in-the-number-of-posts-to-fetch-with-blogger-api-error-400