I need to get the amount of emails sent and received in a Google Workspace.
Basically, I want to know if it is possible to get the data of this report by some Google API. I get this print directly from Google Admin.
I tried using Gmail API.
const { google } = require('googleapis')
const oAuth2Client = require('./oauth.js')
const gmail = google.gmail({
version: 'v1',
auth: oAuth2Client
});
await gmail.users.messages.list({ userId: 'me' }) // success
await gmail.users.messages.list({ userId: 'othermail@domain.com' }) // permission error
But I can only get the emails from the authenticated account. And I need from all accounts in domain.
To list all messages in every account, you will need to use domain-wide delegation to impersonate the account.
However, it's easier and more performant to find the closest property in the Reports API that fits what you need, like num_emails_exchanged
.