Search code examples
htmlrestgoogle-apiblogger

Limitation in the number of posts to fetch with Blogger API (error 400)


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


Solution

  • 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.