Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Credential implementation provided to initializeApp() via the "credential" property failed to fetch a valid Google OAuth2 access token

    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;

then msg


> (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".
> 

I tried making the env value a string and not a string as well as checking to see if the process.env was being detected and still nothing not sure at this point I have googled to no avail please give some pointers or tips **hint hint maybe a solution haha :)

like image 239
Yordani Bonilla Avatar asked Oct 06 '19 22:10

Yordani Bonilla


1 Answers

Try this one

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

changing the applicationDefault to cert

like image 102
BINFAS K Avatar answered Oct 19 '22 03:10

BINFAS K