首页 > 解决方案 > 带有 Firebase Id 令牌的 Firestore REST API

问题描述

我正在尝试使用从Auth SDK(ios,但实际上我将FirebaseAuth用于osx,这就是为什么除了使用REST之外我别无选择,因为没有FirebaseFirestore对osx的支持)在firestore创建新文档)。

规则是

service cloud.firestore {
  match /databases/{database}/documents {    
    match /places {
      allow create, update, delete, read: if true;
    }
  }
}

用于检索令牌

Auth.auth().currentUser?.getIDTokenForcingRefresh(true, completion ..

在我将令牌作为字符串并将其放入标头 Authorization: Bearer MY_RETRIEVED_TOKEN 之后

链接我请求

https://firestore.googleapis.com/v1beta1/projects/MYPROJECTID/databases/(default)/documents/places?fields=fields&documentId=kk&key=API_KEY_FOR_WEB_APP_FROM_FIREBASE_PROJECT_SETTING

所以在这里我从 Firebase 项目设置为 Web 传递密钥

此 POST 请求的正文

{
  "fields": {
    "nn": {
        "stringValue": "hhh"
    }
  }
}

但最后我得到了 403 作为回应。

{
  "error": {
    "code": 403,
    "message": "Missing or insufficient permissions.",
    "status": "PERMISSION_DENIED"
  }
}

已经打破了我的想法,试图找到一个错误。提前致谢!

标签: iosmacosfirebasegoogle-cloud-firestore

解决方案


几天前我遇到了同样的问题。

您应该尝试修改您的规则,如下面的代码:

service cloud.firestore {
  match /databases/{database}/documents {    
    match /places/{document=**} {
      allow create, update, delete, read: if true;
    }
  }
}

在这种情况下,您为所有文档设置规则places。您也可以在规则模拟器中测试您的规则。这工作得很好。

我希望这能帮到您


推荐阅读