0

I'm having some trouble accessing my firestore data via the python API. I've tried all the solutions that are mentioned in the other answers, but I still keep getting the same permission denied issue:

_InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
    status = StatusCode.PERMISSION_DENIED
    details = "Missing or insufficient permissions."
    debug_error_string = "{"created":"@1636057424.408000000","description":"Error received from peer ipv4:x:x:x:x:443","file":"src/core/lib/surface/call.cc","file_line":1068,"grpc_message":"Missing or insufficient permissions.","grpc_status":7}"
>

Here's how I'm using the API:

import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore


cred = credentials.Certificate('/users/88232/cred.json') #correct credentials path - also tried pasting the dictionary directly
firebase_admin.initialize_app(cred)
db = firestore.Client('myapp') #made sure it's the right project's name (I have multiple projects)
doc_ref = db.collection(u'sample').document(u'0SZxt2xXs7n8Vglz8wEu')
doc = doc_ref.get() #error here

Things I've tried:

  • Use the service account credentials from firebase console in the API [1]
  • Include data store owner as part of roles when creating a new service account and using these credentials in the API [2]
  • Made sure Rules say allow read, write: if true; [3]

I'm not sure what else to try. Can someone please let me know if I have missed something?

enter image description here

enter image description here

user1934283
  • 124
  • 4
  • 15
  • Hi @user1934283, I already replicated your issue. Based on my replication, there are two possible scenario that you can encounter that error. 1. Could you make sure that you are using the correct project ID. Go to [Google Cloud Console](https://console.cloud.google.com), click on the drop down button at the top navigation bar and choose the correct project ID that you are using. 2. Please also check your "cred" is correct. I'm able to replicate the error if I intentionally put the wrong "cred" or the project ID. – Marc Anthony B Nov 05 '21 at 07:18
  • @MarcAnthonyB I figured out the issue - I've posted it as the answer. It's to do with using `client(...)` instead of `Client(...)`. Also, I have double-checked the project ID and the credentials. – user1934283 Nov 05 '21 at 08:30

1 Answers1

1

I figured it out. Apparently, the issue was in this line:

db = firestore.Client('myapp')

if I change this to:

db = firestore.client()

everything works just as expected.

This is also the only method shown in Google's official docs (https://firebase.google.com/docs/reference/admin/python/firebase_admin.firestore).

I'm not sure what guide/walkthrough I followed that made me use Client instead of client but I'm not able to figure out the difference. If anyone knows, please comment below.

user1934283
  • 124
  • 4
  • 15