Firestore:代码16(请求具有无效的身份验证凭据)

时间:2018-11-01 22:00:38

标签: javascript node.js firebase google-cloud-firestore firebase-admin

我正在尝试通过私钥和管理员权限以管理员权限访问Firestore,以便可以将其用于后端的查询和内容。

//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

这似乎与文档中的说明完全一样,但是我的问题是,在尝试执行Node的某些计算机上,我只能设法随机连接几台。

有时我会连接并且运行良好,并按应有的方式打印可用的集合,但是大多数时候,我得到的都是错误的凭据无效错误

{
  "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"
}

我尝试不进行任何更改,并且有时在某些我测试完全相同的代码的计算机上也可以使用,但是在其他计算机上却不能,并且尝试通过firebase控制台获取新的私钥对此没有任何改变。当我尝试连接时,有时会花费很长时间才出现此错误,否则它根本不会回叫。提供的链接也无济于事,因为它只提供了一些JS代码供通过前端进行常规访问以登录google,这在这种情况下根本不是我的用例(我已经尝试过“配置项目”按钮)。

我在这里做错什么吗?为什么有时什么也没做,有时却什么都不做,为什么有时能起作用?

我非常感谢您

1 个答案:

答案 0 :(得分:1)

此错误与该错误(Not able to send device push notification due to app/invalid-credential error)基本上相同,在此错误中,计算机的时钟需要与Firebase服务器的时钟同步。

在一个朋友试图指出这一点之前,我一直无法在各种计算机上测试它(所有时钟设置错误)而无法连接,所以我进行了测试并成功了。

最适合我的解决方案是检查计算机上的clock设置。