首页 > 解决方案 > google.auth.GoogleAuth() 需要什么 keyFile 密钥?

问题描述

目标

将 googleapis 与 firebase 功能一起使用。获取 JWT 令牌,以便 firebase 函数可以使用具有域范围委派的服务帐户来授权 G Suite API,例如目录和驱动器。

问题

path.join() 中发生了什么;

什么是 __dirname 什么 'jwt.keys.json'

从这个例子: https ://github.com/googleapis/google-api-nodejs-client/blob/master/samples/jwt.js

  // Create a new JWT client using the key file downloaded from the Google Developer Console
  const auth = new google.auth.GoogleAuth({
    keyFile: path.join(__dirname, 'jwt.keys.json'), // <---- WHAT GOES IN path.join()
    scopes: 'https://www.googleapis.com/auth/drive.readonly',
  });

错误

当我跑

  const auth = new google.auth.GoogleAuth({
    keyFile: path.join(__dirname, "TEST"), // <-- __dirname == /srv/ at runtime
    scopes: 'https://www.googleapis.com/auth/drive.readonly',
  });

从 GCP 日志中,我收到此错误:

Error: ENOENT: no such file or directory, open '/srv/TEST'

显然TEST是无效的,但是'/srv/?

keyFile文件路径是什么?凭证?

另一个例子

https://github.com/googleapis/google-api-nodejs-client#service-to-service-authentication

标签: firebase-authenticationgoogle-cloud-functionsgoogle-api-nodejs-client

解决方案


我在这里找到了文档:

https://googleapis.dev/nodejs/google-auth-library/5.10.1/classes/JWT.html

如果您不想包含文件,您可以在请求授权时使用keykeyIdemail提交凭据。


推荐阅读