Avi Vaulin
Avi Vaulin

Reputation: 89

How to fetch traffic stats for a domain by date from gmailpostmastertools?

I'm trying to fetch traffic stats by date for a domain through gmailpostmastertools over OAuth2, I'm able to retrieve postmaster.domains.list and traffic stats for a domain without any date parameters, when any of the endDate or startDate parameters included in the request, it throws the following,

Error: Request contains an invalid argument.
at Gaxios._request (C:\projects\postmaster-api\node_modules\gaxios\src\gaxios.ts:158:15)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at OAuth2Client.requestAsync (C:\projects\postmaster-api\node_modules\google-auth-library\build\src\auth\oauth2client.js:345:18)

Here's the condensed bit that tries to fetch the data,

import { google } from 'googleapis';

oauth2Client = new google.auth.OAuth2(
  'client_placeholder',
  'secret_placeholder', 
  '/oauth2callback'
);

postmaster = google.gmailpostmastertools({
  version: 'v1',
  auth: oauth2Client
});

await this.postmaster.domains.trafficStats.list({ 
  'startDate.day'   : 1,
  'startDate.month' : 7,
  'startDate.year'  : 2021,
  parent : `domains/${domain_name}`
});

It works fine to fetch the data directly with access_token.

let access_token = 'my_access_token';
let domain_name = 'mydomain.com';
let date = '20210701';
await fetch(`https://gmailpostmastertools.googleapis.com/v1/domains/${domain_name}/trafficStats/${date}?access_token=${access_token}`, {
    method: 'GET'
});

Any thoughts?

Upvotes: 0

Views: 620

Answers (1)

evan_b
evan_b

Reputation: 1249

You need to specify both startDate and endDate in the request.

From https://developers.google.com/gmail/postmaster/reference/rest/v1/domains.trafficStats/list

startDate: The earliest day of the metrics to retrieve inclusive. startDate and endDate must both be specified or both left unspecified. If only one is specified, or endDate is earlier in time than startDate, then INVALID_ARGUMENT is returned.

Upvotes: 0

Related Questions