Search code examples
firebasegoogle-cloud-firestorepostmanfirebase-security

POST in firestore DB using only API Key as Auth


in my firebase I set as authentification "Anonym" and want to try a POST to my firestore using the project API key as follow:

POST /v1/projects/myproject123-62cd3/databases/(default)/documents/sammlung2/test321 HTTP/1.1
Host: firestore.googleapis.com
Content-Type: application/json
Authorization: Bearer apikey3123idsn3ejhsd3
Content-Length: 111

{
  "fields": {
    "field1": { "stringValue": "value1" },
    "field2": { "stringValue": "value2" }
  }
}

Firestore rules are:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow write, read: if request.auth.uid == 'API_KEY';
    }
  }
}

As response I get "Request had invalid authentication credentials.". What am I doing wrong here?


Solution

  • There is no way to post to Firestore with the API key of your project. The bearer token in the Authorization header has to either be the ID token of a Firebase Authentication user, or the OAuth2 token for a collaborator on the project.

    Also see the Firebase documentation on authentication and authorization with the REST API for Firestore, which says:

    For authentication, the Cloud Firestore REST API accepts either a Firebase Authentication ID token or a Google Identity OAuth 2.0 token.