phpgoogle-apps-scriptgoogle-apigoogle-postmaster

Google Postmaster API - how to pass startDate parameter?


Here is my code to get the traffic stats from my Google Postmaster account. It works perfectly without the date parameters, but as soon as I add a startDate it doesn't work anymore

$url = 'https://gmailpostmastertools.googleapis.com/v1/domains/'.$this_domain.'/trafficStats/?pageSize=9999&startDate=20221001&endDate=20221030';

$ch = curl_init();
$headers = array(
  'Authorization: Bearer ' . $token,
  'Accept: application/json'
);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING , "");
curl_setopt($ch, CURLOPT_TIMEOUT, 30);

$google_postmaster_api_json = curl_exec($ch);


$google_postmaster_api = json_decode($google_postmaster_api_json, true);
echo "<pre>";
var_dump($google_postmaster_api);
echo "</pre>";

if the StartDate parameter is added, the API returns this error:

Invalid JSON payload received. Unknown name "startDate": Cannot bind query parameter. 'startDate' is a message type. Parameters can only be bound to primitive types.
Invalid JSON payload received. Unknown name "endDate": Cannot bind query parameter. 'endDate' is a message type. Parameters can only be bound to primitive types.

If I try to POST it as a json instead, the result returned is NULL even if there should be data returned

      $json_request = '{
        "dateRanges": [{ "startDate": "2022-10-01", "endDate": "2022-10-15" }],
      }';

      $ch = curl_init();
      $headers = array(
        'Authorization: Bearer ' . $token,
        'Accept: application/json'
      );
      curl_setopt($ch, CURLOPT_POSTFIELDS, $json_request);
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
      curl_setopt($ch, CURLOPT_HEADER, 0);
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
      curl_setopt($ch,CURLOPT_POST,1);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_ENCODING , "");
      curl_setopt($ch, CURLOPT_TIMEOUT, 30);

      $google_postmaster_api_json = curl_exec($ch);

Solution

  • As per the documentation (although I have to say it's not 100% clear) the "startDate" and "endDate" parameters are actually composites, and to generate a valid request you must specify each component of those dates (year, month and day) as separate parameters in your URL, in this format:

    startDate.day=X&startDate.month=X&startDate.year=X&endDate.day=X&endDate.month=X&endDate.year=X
    

    (where X is of course replaced by a valid numeric value for each parameter).

    The documentation entries for "startDate" and "endDate" show the type as "Object (Date)", where the "Date" is a link to the Date section of the documentation where it describes the individual fields which make up a date for the purposes of the API requests.

    I was able to confirm this by using the "Try this method" part of the documentation to generate a request to the API, and the Network tool in my browser showed the URL it generated as a result of submitting the request from the form, and the date portions of the URL querystring matched the format I've shown above.


    P.S. It definitely does not support sending the data in JSON format via a POST request though. The documentation is clear that it must be a GET request and that the request body must be empty.