Skip to content

Trying to read from db using admin - Error fetching access token: Unexpected response while fetching access token: \"\". #1826

Description

@naeri-kailash

[REQUIRED] Step 2: Describe your environment

  • Operating System version: MacOS Monterey
  • Firebase SDK version: 11.3.0
  • Firebase Product: auth, realtime database
  • Node.js version: v17.8.0
  • NPM version: 8.5.5

[REQUIRED] Step 3: Describe the problem

Steps to reproduce:

What happened? How can we make the problem occur?
This could be a description, log/console output, etc.

I am trying to connect to my Firebase Realtime DB in my Typescript app that is using JS modules. I get an error that I'm uncertain how to interpret. I have used the same credentials elsewhere and they work so i believe the issue is in my code. This is a basic JS app, it isn't a firebase functions or hosting product but I am under the impression that should not matter.

Relevant Code:

import admin from "firebase-admin";
const serviceAccount = {
"projectId": "xxxproject-namexxx",
"privateKey": "-----BEGIN PRIVATE KEY-----\nxxxxxxxxxxxxREDACTEDxxxxxxxxx==\n-----END PRIVATE KEY-----\n",
"clientEmail": "firebase-adminsdk-xxxx@redacted.iam.gserviceaccount.com"
}
admin.initializeApp({
credential: admin.credential.cert({
projectId: serviceAccount.projectId,
clientEmail: serviceAccount.clientEmail,
privateKey: serviceAccount.privateKey,
}),
databaseURL: "https://redacted-rtdb.firebaseio.com"
});
const data = admin.database();
const toRead = `/elephants/males`
const ref = data.ref(toRead);
console.log('making request')
ref.once("value", function(snapshot) {
const resp = snapshot.val()
console.log('resp', resp)
})
.catch(err => {
console.log('Error: ', err.message);
});

What happens:

npm start
>my-project@1.0.0 start
> mkdir -p db && tsc -p tsconfig.json && node --experimental-specifier-resolution=node build/src/index.js
(node:88167) ExperimentalWarning: The Node.js specifier resolution flag is experimental. It could change or be removed at any time.
(Use `node --trace-warnings ...` to show where the warning was created)
making request
[2022-07-21T02:17:38.935Z] @firebase/database: FIREBASE WARNING: {"code":"app/invalid-credential","message":"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: Unexpected response while fetching access token: \"\"\"."}
[2022-07-21T02:17:39.502Z] @firebase/database: FIREBASE WARNING: {"code":"app/invalid-credential","message":"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: Unexpected response while fetching access token: \"\"\"."}
[2022-07-21T02:17:40.056Z] @firebase/database: FIREBASE WARNING: {"code":"app/invalid-credential","message":"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: Unexpected response while fetching access token: \"\"\"."}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions