Wilker
Wilker

Reputation: 131

Firestore: code 16 (Request had invalid authentication credentials)

i am trying to access Firestore with admin permissions via private key and admin permissions so i can use it for queries and stuff in the backend.

//i import fb-admin
const firebaseAdmin = require('firebase-admin')

//i initialize as admin with a valid credential
firebaseAdmin.initializeApp({
  credential: firebaseAdmin.credential.cert('./pathto/myprivatekeyinfo.json')
})

//i initialize firestore
var dba = firebaseAdmin.firestore()

//i test the connection by listing all the available collections
dba.getCollections().then(collectionList => {
  collectionList.forEach(collection => {
    console.log(collection.id) //print available collections
  }
},error => console.error(JSON.stringify(error,null,"  "))) //print error

It seems exactly like instructed in the documentation, but my problem is that with some computers that i try to execute Node in, i only manage to connect at random on a few.

Sometimes i connect and it goes fine, printing the available collections as it should, but most of the time all i get is this error for invalid credentials

{
  "code": 16,
  "metadata": {
    "_internal_repr": {
      "www-authenticate": [
        "Bearer realm=\"https://accounts.google.com/\""
      ]
    }
  },
  "details": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
  "note": "Exception occurred in retry method that was not classified as transient"
}

i've tried changing nothing and it works sometimes on some computers where i test the exact same code, but in others it doesn't, and trying to get a new private key via the firebase console changes nothing in the matter. When i try to connect it sometimes takes too long just to get this error or it does not call back at all. The link provided isn't helpful either, as it only gives some JS code for regular access via the front end to login with google which isn't my use case at all in this situation (i've tried the "configure a project" button too).

is there anything that i am doing really wrong here? why does it work sometimes although changing nothing and sometimes it just doesn't?

i appreciate any help

Upvotes: 2

Views: 1623

Answers (1)

Wilker
Wilker

Reputation: 131

This error is basically the same to this one (Not able to send device push notification due to app/invalid-credential error), where the clock of the computer needs to be synced to the clock of the firebase server.

i was stuck trying to test it in various computers (all with the wrong set up clock) without being able to connect before a friend tried to point out that, so i tested and it worked.

The solution that worked for me was definitely to check the set up clock on the computer.

Upvotes: 6

Related Questions