Firebase错误通过函数写入Firestore:" 7 PERMISSION_DENIED:权限丢失或不足"

时间:2018-05-23 13:18:32

标签: typescript firebase google-cloud-firestore google-cloud-functions

我尝试编写一个简单的Firebase函数,该函数使用TypeScript向Firestore添加记录。代码的缩减版本是:

import * as admin from "firebase-admin";
import * as functions from "firebase-functions";

const config = functions.config();

admin.initializeApp(functions.config().firebase);
admin.database.enableLogging(true);

exports.subscriptions = functions.https.onRequest(
  async (req, res) => {
    try {
      const requestSubscription = req.body;
      const subscription = {
        subscription_id: requestSubscription.subscription_id,
        email: requestSubscription.email || ""
      };

      const id = subscription.subscription_id;
      const dbSubscriptions = admin.firestore().collection("subscriptions");
      await dbSubscriptions.doc(id).set(subscription);

      return res.status(200).send(`Success`);
    } catch (e) {
      console.error(e.stack);
      return res.status(500).send(`Error`);
    }
  }
);

这在本地工作正常。部署时,我收到此错误:

Error: 7 PERMISSION_DENIED: Missing or insufficient permissions.
    at Object.exports.createStatusError (/user_code/node_modules/firebase-admin/node_modules/grpc/src/common.js:87:15)
    at Object.onReceiveStatus (/user_code/node_modules/firebase-admin/node_modules/grpc/src/client_interceptors.js:1214:28)
    at InterceptingListener._callNext (/user_code/node_modules/firebase-admin/node_modules/grpc/src/client_interceptors.js:590:42)
    at InterceptingListener.onReceiveStatus (/user_code/node_modules/firebase-admin/node_modules/grpc/src/client_interceptors.js:640:8)
    at callback (/user_code/node_modules/firebase-admin/node_modules/grpc/src/client_interceptors.js:867:24)

我的函数/ package.json文件是:

{
  "name": "functions",
  "scripts": {
    "lint": "tslint --project tsconfig.json",
    "build": "tsc",
    "serve": "npm run build && firebase serve --only functions",
    "shell": "npm run build && firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "main": "lib/index.js",
  "dependencies": {
    "express": "4.16.3",
    "firebase-admin": "5.12.1",
    "firebase-functions": "1.0.3"
  },
  "devDependencies": {
    "tslint": "^5.8.0",
    "typescript": "^2.5.3"
  },
  "private": true
}

我搜索此错误的唯一链接是这个未解决的问题: PERMISSION_DENIED Firestore CloudFunction TypeScript

我设置了我的Firestore规则,允许任何人阅读和写入,即使以上是使用管理员,但它仍然无法正常工作。

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

我与Firebase支持人员就此进行了联系,他们也不知道发生了什么。我创建了一个新项目,已部署并且有效。也许这只是一个奇怪的新项目故障。

答案 1 :(得分:0)

我的项目首先是在 Google Cloud 上创建的,然后将该项目连接到 Firebase。

就我而言,问题是 {project-id}@appspot.gserviceaccount.com 没有 Editor 角色,一旦我为该服务帐户添加了编辑者角色,它就开始工作

ps 同样是第一次,用户应该是 Owner 才能创建 Firestore 数据库,否则您会在 Firestore 页面 To manage Cloud Firestore, ask a project owner for the necessary permissions

中看到此消息