关于用PHP分页gmail api

时间:2015-11-30 17:24:58

标签: php laravel gmail gmail-api

我在“我的应用”中成功列出了这些消息。现在我需要对消息进行分页 (每页大约8个)。我按照谷歌的文档,但它只给出了一个pageToken,但如何让它工作?

1 个答案:

答案 0 :(得分:2)

如果您list messages,并且仍有更多搜索结果,则回复将包含nextPageToken

请求

userId = me
maxResults = 8

GET https://www.googleapis.com/gmail/v1/users/me/messages?maxResults=8&access_token={YOUR_API_KEY}

<强>响应

{
 "messages": [
  {
   "id": "151596a5055b9412",
   "threadId": "151596a5055b9412"
  },
  {
   "id": "1515915d0fcfd685",
   "threadId": "1515915d0fcfd685"
  },
  {
   "id": "15158e6826ed7587",
   "threadId": "15158e6826ed7587"
  },
  {
   "id": "15158e0e37572671",
   "threadId": "15158e0e37572671"
  },
  {
   "id": "151586443b5b309a",
   "threadId": "151586443b5b309a"
  },
  {
   "id": "15157c4b11732c5c",
   "threadId": "1510f004b81a9de2"
  },
  {
   "id": "151576512d37c9ec",
   "threadId": "1515765122918d37"
  },
  {
   "id": "1515765122918d37",
   "threadId": "1515765122918d37"
  }
 ],
 "nextPageToken": "01770178536732383613", // Here it is!
 "resultSizeEstimate": 26
}

只需在下一个请求中将此值包含为pageToken

请求

userId = me
maxResults = 8
pageToken = 01770178536732383613

GET https://www.googleapis.com/gmail/v1/users/me/messages?maxResults=8&pageToken=01770178536732383613&access_token={YOUR_API_KEY}

<强>响应

{
 "messages": [
  {
   "id": "1515762549119366",
   "threadId": "1513f096ab90fdab"
  },
  {
   "id": "15157616d03a66a1",
   "threadId": "1513f096ab90fdab"
  },
  {
   "id": "151575f958ac69e8",
   "threadId": "1513338849950602"
  },
  {
   "id": "1515756737710843",
   "threadId": "1515756737710843"
  },
  {
   "id": "1515756735412b45",
   "threadId": "1515756735412b45"
  },
  {
   "id": "1515756710eed602",
   "threadId": "1515756710eed602"
  },
  {
   "id": "15157567089a24b0",
   "threadId": "15157567089a24b0"
  },
  {
   "id": "151574a87fefe71d",
   "threadId": "151336e890f46a2c"
  }
 ],
 "nextPageToken": "13534757816909071635",
 "resultSizeEstimate": 27
}

当响应中没有nextPageToken时,您已获取每个结果。