Manuel
Manuel

Reputation: 1

Error 403 when trying to generate adsense earnings report using API in Node

I want to generate a report from a firebase cloud function (in Node), I am using the Adsense API v2 like this:

    const { google } = require("googleapis");
    const adsense = google.adsense("v2");

    const auth = new google.auth.GoogleAuth({
      scopes: ["https://www.googleapis.com/auth/adsense", "https://www.googleapis.com/auth/adsense.readonly"],
    });

    const authClient = await auth.getClient();
    google.options({ auth: authClient });

    const res = await adsense.accounts.reports.generate({
      account: "accounts/pub-1522817738845966",
      dateRange: "YESTERDAY",
      metrics: ["TOTAL_EARNINGS"],
      dimensions: ["DATE"],
    });

But I get the following error: code: 403, message: 'The caller does not have permission'.

I want to run this function every day to check the total earnings from the previous day. I made sure the API was enabled in Google Cloud console and the adsense account is the same as the firebase admin account.

If I put the same values in the API explorer I get this response, which doesn't give me the information that I want but atleast is not an error 403:

200
{
  "headers": [
    {
      "name": "DATE",
      "type": "DIMENSION"
    },
    {
      "name": "TOTAL_EARNINGS",
      "type": "METRIC_CURRENCY",
      "currencyCode": "EUR"
    }
  ],
  "warnings": [
    "Some of the requested metrics are not available for some of the ad clients used by this report."
  ],
  "startDate": {
    "year": 2023,
    "month": 4,
    "day": 6
  },
  "endDate": {
    "year": 2023,
    "month": 4,
    "day": 6
  }
}

Upvotes: 0

Views: 199

Answers (1)

Manuel
Manuel

Reputation: 1

Apparently Adsense management API doesn't support service accounts, you can only use it with an Oauth 2 authentication flow

Upvotes: 0

Related Questions