googleapis / google-api-php-client

A PHP client library for accessing Google APIs
Apache License 2.0
9.22k stars 3.52k forks source link

Invalid FieldMask error while fetching batched gmail get responses #2203

Open mohsin opened 2 years ago

mohsin commented 2 years ago

I'm trying to make batched requests more efficient by getting partial responses. I'm interested to get just certain headers (from, to, subject) and the message body.

My code works fine when I run it like this:

$client = getClient();
$client->setUseBatch(true);
$service = new Google_Service_Gmail($client);

$batch = new Google_Http_Batch($client, false, null, 'batch/gmail/v1');
$request1 = $service->users_messages->get('me', '17eb57d85dd4933d', ['format'=>'full', 'fields' => 'id,snippet,payload(headers(name,value),body,parts)']);
$request2 = $service->users_messages->get('me', '17eb4fe58a1b3be6', ['format'=>'full', 'fields' => 'id,snippet,payload(headers(name,value),body,parts)']);
$batch->add($request1, "message" . '-17eb57d85dd4933d');
$batch->add($request2, "message" . '-17eb4fe58a1b3be6');
$result = $batch->execute();
var_dump($result);

As you can see in the above request, I'm getting ALL the header name, value pairs as payload(headers(name,value). But when I change the code to select only certain headers (I'm interested only in From, To, Subject).

$request1 = $service->users_messages->get('me', '17eb57d85dd4933d', ['format'=>'full', 'fields' => 'id,snippet,payload(headers(name,value)[name=From,To,Subject],body,parts)']);

The API throws me an error:

Invalid FieldMask 'id,snippet,payload(headers(name,value)[name=From,To,Subject],body,parts)'. Map keys should be represented as ["some_key"].

I tried changing it to headers(name,value)[name="From",name="To",name="Subject"] and a few other formats but cannot figure out how to get it right. The API documentation too doesn't go in-depth to discuss how to select specific headers in key-value pairs so some help would be appreciated. Thanks.

LindaLawton commented 2 years ago

As far as i know you cant map on keys like that use this I have never heard of it and cant really find anything in the docs about it. Would be awesome if it did work though.

This works:

   id,snippet,payload(headers,body,parts)

you should use the Try me on this page if you want to test it out though.