使用 JSON 对象而不是 JSON 文件进行 Google API 身份验证 - Node.js

buz*_*j99 6 javascript json google-api google-authentication node.js

我目前正在使用以下代码作为服务帐户进行身份验证:

const auth = new google.auth.GoogleAuth({
    keyFile: "<path/to/credentials>",
    scopes: [<scopes>],
});
Run Code Online (Sandbox Code Playgroud)

有没有办法使用包含与 keyFile 相同内容的 JSON 对象进行身份验证?例如,做这样的事情:

const credentials = {
  "type": ...,
  "project_id": ...,
  "private_key_id": ...,
  "private_key": ...,
  "client_email": ...,
  "client_id": ...,
  "auth_uri": ...,
  "token_uri": ...,
  "auth_provider_x509_cert_url": ...,
  "client_x509_cert_url": ...
}

const auth = new google.auth.GoogleAuth({
  keyFile: credentials,
  scopes: [<scopes>],
});
Run Code Online (Sandbox Code Playgroud)

buz*_*j99 8

更改keyFile: "<path/to/credentials>",为credentials: credentials可以解决问题,因为 GoogleAuthOptions 也可以与 CredentialBody 一起使用。

来源:Google Cloud Node.js 文档