node.js - Node.js 中的 Firestore 管理员缺少或权限不足

标签 node.js firebase google-cloud-firestore google-cloud-functions firebase-cli

我正在尝试在 Firebase 云函数中使用 Node.js v8.4.0 上的 Firebase Admin 访问 Firestore,在 Windows 10 上使用 firebase functions:shell 在本地运行。

firebase -v 4.2.1

  "dependencies": {
    "firebase-admin": "^6.0.0",
    "firebase-functions": "^2.0.5"
  }

尝试从我的应用程序代码中使用 firebase admin 后,我尝试运行 https://github.com/firebase/functions-samples/blob/master/quickstarts/uppercase-firestore/functions/index.js 中的快速入门示例.

这是实际运行的代码:

'use strict';

// [START all]
// [START import]
// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');

// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');

admin.initializeApp({})
// [END import]

// [START addMessage]
// Take the text parameter passed to this HTTP endpoint and insert it into the
// Realtime Database under the path /messages/:documentId/original
// [START addMessageTrigger]
exports.addMessage = functions.https.onRequest((req, res) => {
// [END addMessageTrigger]
  // Grab the text parameter.
  const original = req.query.text;
  // [START adminSdkAdd]
  // Push the new message into the Realtime Database using the Firebase Admin SDK.
  return admin.firestore().collection('messages').add({original: original}).then((writeResult) => {
    // Send back a message that we've succesfully written the message
    return res.json({result: `Message with ID: ${writeResult.id} added.`});
  });
  // [END adminSdkAdd]
});
// [END addMessage]

// [START makeUppercase]
// Listens for new messages added to /messages/:documentId/original and creates an
// uppercase version of the message to /messages/:documentId/uppercase
// [START makeUppercaseTrigger]
exports.makeUppercase = functions.firestore.document('/messages/{documentId}')
    .onCreate((snap, context) => {
// [END makeUppercaseTrigger]
      // [START makeUppercaseBody]
      // Grab the current value of what was written to the Realtime Database.
      const original = snap.data().original;
      console.log('Uppercasing', context.params.documentId, original);
      const uppercase = original.toUpperCase();
      // You must return a Promise when performing asynchronous tasks inside a Functions such as
      // writing to the Firebase Realtime Database.
      // Setting an 'uppercase' sibling in the Realtime Database returns a Promise.
      return snap.ref.set({uppercase}, {merge: true});
      // [END makeUppercaseBody]
    });
// [END makeUppercase]
// [END all]

但是,我仍然收到权限被拒绝的错误。

这是我得到的输出:

firebase > makeUppercase({original:'alphabets'},{params:{documentId:'mydoc'}})
'Successfully invoked function.'
firebase > info: User function triggered, starting execution
info: Uppercasing mydoc alphabets
info: Function crashed
info: { Error: 7 PERMISSION_DENIED: Missing or insufficient permissions.
    at Object.exports.createStatusError (C:\projects\myproject\functions\node_modules\grpc\src\common.js:87:15)
    at Object.onReceiveStatus (C:\projects\myproject\functions\node_modules\grpc\src\client_interceptors.js:1188:28)
    at InterceptingListener._callNext (C:\projects\myproject\functions\node_modules\grpc\src\client_interceptors.js:564:42)
    at InterceptingListener.onReceiveStatus (C:\projects\myproject\functions\node_modules\grpc\src\client_interceptors.js:614:8)
    at callback (C:\projects\myproject\functions\node_modules\grpc\src\client_interceptors.js:841:24)
  code: 7,
  metadata: Metadata { _internal_repr: {} },
  details: 'Missing or insufficient permissions.' }

我的安全规则是完全开放的,但并没有解决错误。

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write;
    }
  }
}

我还认为这可能是身份验证问题,所以我尝试了以下方法来初始化应用程序:

1

admin.initializeApp()

2

admin.initializeApp(functions.config().firebase);

3

var serviceAccount = require('path/to/serviceAccountKey.json');

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: 'https://<DATABASE_NAME>.firebaseio.com'
});

最后一个带有我自己的凭据文件和项目配置。所有这些尝试仍然给我缺少权限错误。

更新: 我将这些功能部署到云端,它们似乎运行良好,但在本地运行时,我仍然收到错误:7 权限被拒绝。

更新 2: 根据@Doug Stevenson 的建议,使用 gcloud auth application-default login 设置应用程序默认凭据。确保未设置环境变量 GOOGLE_APPLICATION_CREDENTIALS。尝试了上面的 1,2 和 3 以及下面的 4 中的代码,但没有成功。遇到同样的错误。

4

admin.initializeApp({
    credential: admin.credential.applicationDefault(), 
    databaseURL: "https://myapp-redacted.firebaseio.com"
  });

最佳答案

我希望现在您已经解决了您的问题。同样的事情刚刚发生在我身上,我正在分享我的情况,希望它能帮助其他人。

我们管理多个 firebase 项目 - 制作、开发、暂存等。

我们用这个初始化 admin sdk:

let serviceAccount = require("../serviceAccountKey.json");
const databaseUrl = functions.config().environment.databaseurl
const storageBucket = functions.config().environment.storagebucket
const isDev = "true" === functions.config().environment.dev

// if dev environment
if (isDev) {
  serviceAccount = require("../serviceAccountKey-dev.json");
}

admin.initializeApp({
    projectId: functions.config().environment.projectid,
    credential:    admin.credential.cert(serviceAccount),
    databaseURL:   databaseUrl,
    storageBucket: storageBucket
});

你知道你需要怎么做

firebase functions:config:get > .runtimeconfig.json

让仿真工作。好吧,我的 runtimeconfig 包含错误的配置。我正在加载我的 serviceAccountKey-dev,但我试图访问另一个项目。第二次我修复了我的运行时配置 - 它对我有用。

关于node.js - Node.js 中的 Firestore 管理员缺少或权限不足,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52207155/

相关文章:

node.js - 如何使用curl 在我的本地主机 Node 服务器上执行GET?

html - 在 HTML 字符串中保留换行符

ios - 如何创建没有数据的新 Firestore 文档?

firebase - Google Core IoT 设备离线事件或连接状态

python - python 的 Firestore 监听器

node.js - 如何计算mongoDB中唯一数据的数量

javascript - dnode 和 nowjs 有什么区别?

node.js - 我可以使用 Dropbox API v2 在我的网络应用程序中实现云存储吗?

android - 从 Intent 启动 Instrumentation 测试

ios - 表格单元格中的 Firebase 观察者是否算作每个实例的观察者?