错误:通过“凭据”属性提供给 initializeApp() 的凭据实现未能获取有效的 Google OAuth2 访问令牌

Yor*_*lla 6 node.js express

    const admin = require('firebase-admin');
    require('dotenv').config()
    // const serviceAccount = require("../service-account-file.json");
    const check = process.env.GOOGLE_APPLICATION_CREDENTIALS;
    //GOOGLE_APPLICATION_CREDENTIALS="/home/byordani93/tuDiagram/service-account-file.json"
    console.log(check, 'mate')

    admin.initializeApp({
      projectId: 'tudiagram',
      credential: admin.credential.applicationDefault(check),
      databaseURL: 'https://tudiagram.firebaseio.com'
    });

    module.exports = admin;
Run Code Online (Sandbox Code Playgroud)

然后留言


> (node:3218) UnhandledPromiseRejectionWarning: Error: Credential
> implementation provided to initializeApp() via the "credential"
> property failed to fetch a valid Google OAuth2 access token with the
> following error: "Error fetching access token: Error while making
> request: The "options.agent" property must be one of type Agent-like
> Object, undefined, or false. Received type string. Error code:
> ERR_INVALID_ARG_TYPE".
> 
Run Code Online (Sandbox Code Playgroud)

我尝试将 env 值设置为字符串而不是字符串,并检查是否检测到 process.env,但此时仍然没有任何不确定性也许是一个解决方案哈哈:)

小智 6

试试这个

    import * as firebase  from 'firebase-admin';
    const  serviceAccount = require("../../serviceAccountKey.json")
    
    firebase.initializeApp({
      credential: firebase.credential.cert(serviceAccount),
      databaseURL: 'https://xxxxxx.firebaseio.com'
    });
Run Code Online (Sandbox Code Playgroud)

applicationDefault更改为cert