Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 使用Google Auth库将服务帐户连接到Admob API时出现问题_Node.js_Google Cloud Platform_Admob_Service Accounts_Google Auth Library - Fatal编程技术网

Node.js 使用Google Auth库将服务帐户连接到Admob API时出现问题

Node.js 使用Google Auth库将服务帐户连接到Admob API时出现问题,node.js,google-cloud-platform,admob,service-accounts,google-auth-library,Node.js,Google Cloud Platform,Admob,Service Accounts,Google Auth Library,我一直在尝试从AWS Lambda连接到Admob API,以便不时自动从报告中提取一些值。我已经成功地访问了一个层,我正在尝试使用它连接到Admob API 我已确保为我的服务帐户提供所有者角色,并已将必要的GOOGLE_应用程序_凭据路径添加到环境变量中 这是我添加到Lambda中的代码: const {GoogleAuth} = require('google-auth-library'); exports.handler = (event, context, callback)

我一直在尝试从AWS Lambda连接到Admob API,以便不时自动从报告中提取一些值。我已经成功地访问了一个层,我正在尝试使用它连接到Admob API

我已确保为我的服务帐户提供所有者角色,并已将必要的GOOGLE_应用程序_凭据路径添加到环境变量中

这是我添加到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上搜索、stack overflow、阅读有关连接Admob API的教程以及阅读库代码来找到我的错误。如果有人能告诉我一个解决方案,我将不胜感激。

问题是您试图使用需要OAuth用户凭据的服务帐户。您需要实现OAuth2流,用户在其中输入他们的Google用户名和密码

关于如何创建node.js OAuth 2客户端,请参考以下Google示例