0

我一直在尝试从 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'
    }
  ]
}

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

4

1 回答 1

1

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

AdMob:请求的授权

有关如何创建 node.js OAuth 2 客户端的信息,请参阅以下 Google 示例。

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

于 2020-02-26T20:52:43.500 回答