Gmail API getting all Gmail Inbox messages limits

2019-07-14 04:10发布

问题:

I am trying to get all the inbox messages in the Gmail API, but it's limiting to 500 per request. I would like to get all the messages, and the current messages in the repository is 1600+ but I am only able to get 500 on one request.

$service = new Google_Service_Gmail($client);
$user = 'me';

$params = array(
    'labelIds' => 'INBOX', 
    'maxResults' => 20000
);

$last_date = mysqli_fetch_array($query);
extract($last_date);


$date = date_create($latest);
$date->modify('-1 day');
$filter_date = date_format($date, 'Y/m/d');
$params['q'] = 'in:inbox after:'.$filter_date ;

$messages = $service->users_messages->listUsersMessages( $user, $params );
$list = $messages->getMessages();
//$list only has 500 messages returned to me.

回答1:

Just to add clarity, this is what's happening. You performed a Users.messages: list which fetches 1600+ messages. Now I'm pretty sure Gmail fetched those 1600+ messages but it can only display 500 messages at a time.

If you want to access the next 501-1000 messages, you need to use the nextPageToken which comes with every successful response. That goes on for the next 1001-1500 messages and so forth.

You can read the nextPageToken mentioned here in Gmail Request body