0

I have managed to sign up a user and properly sign them in shortly after they've signed up all in the same requests. Also in my request, I am trying to store the user information in the Firebase Database using the python package Pyrebase.

I have properly initialized my firebase app as well as my firebase database within my flask app

config = {
    "apiKey": "apikey",
    "authDomain": "proj.firebaseapp.com",
    "databaseURL": "https://proj.firebaseio.com",
    "storageBucket": "proj.appspot.com",
    "messagingSenderId": "930158028418",
    "serviceAccount.json": "path/to/theServiceAccountStuff.json"
}

app = Flask(__name__)
firebase = pyrebase.initialize_app(config)
auth = firebase.auth()
firedb = firebase.database()

I am creating this dictionary

userInfo = {"email": email, "password": password, "stripeID": customer.id, "subscriptionID": subID,"subscription": "active", "startdate": "Today bro"}

And passing it into my set function that is supposed to (or at least I thought it did) save my JSON object into the Firebase Database.

firedb.child("users").child(user['localId']).set(data=userInfo)

I am trying to store users under "users" and then categorize them all with a custom key being their localId.

For some reason every time, I hit the line of code above, I get an error saying this

requests.exceptions.HTTPError: [Errno 401 Client Error: Unauthorized for url: https://myproj.firebaseio.com/users/someid.json] {
  "error" : "Permission denied"
}

These are my database rules in firebase

enter image description here

Is there something I am missing? I have even gone a step further to remove all rules so anyone can access it and I have also put in dummy data just to see if it works. Dummy data is possible because I am manually entering in all the information.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
kygcoleman
  • 734
  • 15
  • 25
  • this link most probably help you for sovling that problem [enter link description here](https://stackoverflow.com/questions/41082171/firebase-permission-denied-with-pyrebase-library) – ATIQ UR REHMAN Mar 17 '19 at 06:08

2 Answers2

2

You are trying to use Pyrebase to write to the Firebase Realtime Database, but your project database is configured to use Cloud Firestore instead.

These databases are not the same.

If you are using Python on a hosted service, consider switching to the Firebase Admin SDK for Python. As you are using a service account, this would be the way to go. This will also allow you to make the choice between using Cloud Firestore and the Realtime Database.

If you are using Python on a client device, even if it's a local web server, you shouldn't be using a service account for security reasons and instead opt for client-side authentication. If you wish to use Pyrebase, at the time of writing, you will need to switch your project's database to use the Realtime Database using the dropdown next to the word "Database" in your screenshot. This is because Cloud Firestore does not currently have a REST API which Pyrebase uses as its backend. See the Firebase Realtime Database documentation for more info.

samthecodingman
  • 23,122
  • 4
  • 30
  • 54
  • So every time I switch my database to the real time db, for some reason it switches back automatically to the Firestore. Maybe that something to do with my initial configuration but I should be able to set it. In the case that there isn't a REST API for Firestore, would I be able to still use Pyrebase for my user authentication server side but use Firebase Admin for all DB transactions? – kygcoleman Mar 17 '19 at 14:04
  • Nevermind! As of now, it is possible to it do this way. I like Pyrebase for the server side authentication since I am using Flask. – kygcoleman Mar 17 '19 at 15:11
1

For me, this worked:

Firebase Console -> Realtime Database -> Rules

Edit Rules:

{
  "rules": {
    ".read": "true", 
    ".write": "true",
  }
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
matteo167
  • 11
  • 1
  • 1
    Hi matteo167. Thanks for your answer. Usually answers with an explanation are more welcomed there. Would you like to add an explanation to your answer? You may improve formatting of your answer as well. – MaxV Nov 25 '20 at 20:06