'google-api-nodejs-client - Service Account credentials authentication issues

I am trying to use the google-api-nodejs library to manage some resources in the google Campaign Manager API.

I have confirmed that we currently have a project configured, and that this project has the google Campaign Manager API enabled (see screenshot at the bottom).

I have tried several ways of authenticating myself (particularly API keys, OAuth2, and Service account credentials). This question will focus on using a Service Account for authentication purposes.

Now, I have generated a new service account keyfile (see screenshot at the bottom)), and I configured my code as follows, following the service-account-credentials section of the library's repo. I've also extended the auth scope to include the necessary scope according to this endpoint API docs

import { assert } from "chai";
import { google } from "googleapis";

it("can query userProfiles using service account keyfile", async () => {
  try {
    const auth = new google.auth.GoogleAuth({
      keyFile:
        "/full-path-to/credentials-service-account.json",
      scopes: [
        "https://www.googleapis.com/auth/cloud-platform",
        "https://www.googleapis.com/auth/dfatrafficking",
        "https://www.googleapis.com/auth/ddmconversions",
        "https://www.googleapis.com/auth/dfareporting",
      ],
    });
    const authClient = await auth.getClient();

    // set auth as a global default
    google.options({
      auth: authClient,
    });

    const df = google.dfareporting("v3.5");

    const res = await df.userProfiles.list({});
    console.log("res: ", res);

    assert(true);
  } catch (e) {
    console.error("error: ", e);
    assert(false);
  }
});

This results in the following error:

{
  "code": 403,
  "errors": [
    {
      "message": "Version v3.5 is no longer supported. Please upgrade to the latest version of the API.",
      "domain": "global",
      "reason": "forbidden"
    }
  ]
}

This is an interesting error, because v3.5 is the latest version of that API (as of 14 April 2022) (This page shows the deprecation schedule: https://developers.google.com/doubleclick-advertisers/deprecation. Notice that v3.3 and v3.4 are deprecated, while v3.5 is not.)

In any case, using a different version of the dfareporting API still result in error:

// error thrown: "Version v3.5 is no longer supported. Please upgrade to the latest version of the API."
const df = google.dfareporting("v3.5");
// error thrown: "Version v3.4 is no longer supported. Please upgrade to the latest version of the API."
const df = google.dfareporting("v3.4");
// error thrown: 404 "The requested URL <code>/dfareporting/v3.3/userprofiles</code> was not found on this server"
const df = google.dfareporting("v3.3");

// Note 1: There are no other versions available

// Note 2: It is not possible to leave the version blank
const df = google.dfareporting();
// results in typescript error: "An argument for 'version' was not provided."

I also tried to query the floodlightActivities API, which failed with an authentication error.

// const res = await df.userProfiles.list({});
const res = await df.floodlightActivities.list({
  profileId: "7474579",
});

This, in it's turn, results in the following error:

{
  "code": 401,
  "errors": [
    {
      "message": "1075 : Failed to authenticate. Google account can not access the user profile/account requested.",
      "domain": "global",
      "reason": "authError",
      "location": "Authorization",
      "locationType": "header"
    }
  ]
}

Now, my question is:

  • am I doing something wrong while trying to authenticate using the service account credentials?
  • Or, is it possible that these endpoints do not support service-account-credentials?
  • Or, is something else going wrong here?

service account key, as seen in google console



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source