使用 Google-Auth-Library 将服务帐户连接到 Admob API 时出现问题

Problems connecting Service Account to Admob API with Google-Auth-Library

我一直在尝试从 AWS Lambda 连接到 Admob API 以不时从报告中自动提取一些值。我已成功将 google-auth-library-nodejs 连接到一个图层,我正在尝试使用它连接到 Admob API。

我已确保为我的服务帐户授予所有者角色,并且已将必要的 GOOGLE_APPLICATION_CREDENTIALS 路径添加到环境变量。

这是我添加到我的 Lambda 中的代码:

const {GoogleAuth} = require('google-auth-library');    
exports.handler = (event, context, callback) => {

    async function main() {
      const auth = new GoogleAuth({
        scopes: ['https://www.googleapis.com/auth/admob.report'],
      });

      const client = await auth.getClient();

      //console.log("client", JSON.stringify(client));

      const url = `https://admob.googleapis.com/v1/accounts`;
      const res = await client.request({ url });
      console.log("res: ", JSON.stringify(res.data));
    }

    main().catch(console.error);
};

当我 运行 代码时,出现以下错误:

ERROR   GaxiosError: Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.
    at Gaxios._request (/opt/nodejs/node_modules/gaxios/build/src/gaxios.js:85:23)
    at processTicksAndRejections (internal/process/task_queues.js:94:5)
    at async JWT.requestAsync (/opt/nodejs/node_modules/google-auth-library/build/src/auth/oauth2client.js:350:18)
    at async main (/var/task/index.js:97:19) {
  response: {
    config: {
      url: 'https://admob.googleapis.com/v1/accounts',
      headers: [Object],
      params: [Object: null prototype] {},
      paramsSerializer: [Function: paramsSerializer],
      validateStatus: [Function: validateStatus],
      responseType: 'json',
      method: 'GET'
    },
    data: { error: [Object] },
    headers: {
      'alt-svc': 'quic=":443"; ma=2592000; v="46,43",h3-Q050=":443"; ma=2592000,h3-Q049=":443"; ma=2592000,h3-Q048=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000',
      'cache-control': 'private',
      connection: 'close',
      'content-encoding': 'gzip',
      'content-type': 'application/json; charset=UTF-8',
      date: 'Wed, 26 Feb 2020 18:41:51 GMT',
      server: 'ESF',
      'transfer-encoding': 'chunked',
      vary: 'Origin, X-Origin, Referer',
      'x-content-type-options': 'nosniff',
      'x-frame-options': 'SAMEORIGIN',
      'x-xss-protection': '0'
    },
    status: 401,
    statusText: 'Unauthorized',
    request: { responseURL: 'https://admob.googleapis.com/v1/accounts' }
  },
  config: {
    url: 'https://admob.googleapis.com/v1/accounts',
    headers: {
      Authorization: 'Bearer [Removed]',
      'User-Agent': 'google-api-nodejs-client/5.10.1',
      'x-goog-api-client': 'gl-node/12.14.1 auth/5.10.1',
      Accept: 'application/json'
    },
    params: [Object: null prototype] {},
    paramsSerializer: [Function: paramsSerializer],
    validateStatus: [Function: validateStatus],
    responseType: 'json',
    method: 'GET'
  },
  code: 401,
  errors: [
    {
      message: 'Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.',
      domain: 'global',
      reason: 'unauthorized'
    }
  ]
}

我一直在尝试通过搜索 google、堆栈溢出、阅读有关连接到 Admob API 的教程以及阅读库的代码来找出我的错误。如果有人能指出我的解决方案,我将不胜感激。

问题是您正在尝试使用需要 OAuth 用户凭据的服务帐户。您需要在用户输入 Google 用户名和密码的位置实施 OAuth 2 流程。

AdMob: Authorization for the request

请参阅以下 Google 示例,了解如何创建 node.js OAuth 2 客户端。

https://github.com/googleapis/google-auth-library-nodejs#oauth2