'Gmail API best way to retrieve message list with already included metadata
I'm trying to retrieve gmail messages using Gmail API and Laravel's HTTP Facade.
I noticed that users.messages.list only returns the message's id and threadId, I understand they did this to make the retrieval faster.
The problem with this is that, In my use-case, I wanted to retrieve each message's payload>headers: name="From" and name="To" to get the sender and the receiver's email address and later group the result by receiver's email address.
Now, what I did was, Initially call the users.messages.list by using GET https://gmail.googleapis.com/gmail/v1/users/{userId}/messages. Now I have access to every message's id.
Next, I then loop through the list and create a new property called meta and store the return value of users.messages.get using GET https://gmail.googleapis.com/gmail/v1/users/{userId}/messages/{id}
My code looks like this:
$list = collect((Http::withToken($user->token)
->get('https://gmail.googleapis.com/gmail/v1/users/[email protected]/messages'))->json());
// $list returns {messages: [{id: 12313, threadId: 15151}, {id: 215151, threadId: 12778}, ...]}
$final_list = $list->map(function ($value) {
$value['meta'] = Http::withToken($user->token)
->get('https://gmail.googleapis.com/gmail/v1/users/[email protected]/messages/{$message_id}');
return $value;
})
// $final list should return {messages: [{id: 12313, threadId: 15151, meta: {...}}, {id: 215151, threadId: 12778, meta: {...}}, ...]}
Now, with this approach gave me error saying:
Symfony\Component\ErrorHandler\Error\FatalError
Maximum execution time of 60 seconds exceeded
I undestand that it gives this error, because each messages contain base64 format of the email's body message. Making it very heavy. I can exclude the base64 from the payload by using metadata scope, BUT by doing this, I wont be able to specify the q query parameter filters.
My question is that, is there a way to only retrieve the metadata without using metadata in the auth scope?
OR
Are there any optimal way of achieving the same result(same in the $final_result sample result)?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
