skip to Main Content

When running this code on my VPS:

import admin from 'firebase-admin'
import serviceAcc from '../config/test.json'

export async function fbAdminSdkTest() {
    try {
        console.log('using credentials')
        console.log(admin.credential.cert(serviceAcc as any))
        console.log('!')

        admin.initializeApp({
            credential: admin.credential.cert(serviceAcc as any),
        })

        const test = await admin.auth().createUser({
            email: '[email protected]',
            emailVerified: false,
            phoneNumber: '+11234567890',
            password: 'secretPassword',
            displayName: 'John Doe',
            photoURL: 'http://www.example.com/12345678/photo.png',
            disabled: false,
        })
        console.log(test)
    } catch (e) {
        console.log(e)
    }
}

I get an error:

FirebaseAppError: 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: .".
    at /home/sun4/sun4portal-backend/node_modules/firebase-admin/lib/app/firebase-app.js:87:19
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async fbAdminSdkTest (/home/sun4/sun4portal-backend/test/fbAdminSdkTest.ts:11:22) {
  errorInfo: {
    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: Error while making request: .".'
  },
  codePrefix: 'app'
}

But on my local machine it works, and I have no idea why it doesnt on my VPS..

The line console.log(admin.credential.cert(serviceAcc as any)) prints the exact same info on both systems.

Does anyone have a clue what could be going on? All I’ve done in the Firebase Console is followed the instructions in "Project Settings > Service Accounts"

Local machine:
Windows 10
Node 20.17
firebase-admin 12.5.0

VPS:
Ubuntu 22
Node 20.17
firebase-admin 12.5.0

Thankful for any help!

2

Answers


  1. Chosen as BEST ANSWER

    With the additional information I got from logging the error directly from within firebase-admin I found this solution, and it works, so the issue was IPv6 not working https://stackoverflow.com/a/78445990


  2. The error on your VPS may be due to network issues. Check if it can access Google’s servers with curl, ensure ca-certificates is installed, and that the system time is correct. Google may throttled or blocked your VPS IP, especially if it’s using IPv6. Also, verify service account permissions and, if needed, regenerate the key.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search