5

I have setup a Firebase account and a database.

I have copied the config with the API key into my Python code.

I still get a 401 Permission denied error on Python 3.5

import pyrebase
config = {
"apiKey": "*****",
"authDomain": "***-bot.firebaseapp.com",
"databaseURL": "https://***-bot.firebaseio.com",
"storageBucket": "ebo-bot.appspot.com"
}

firebase = pyrebase.initialize_app(config)
db = firebase.database()
data = {"name": "Mortimer 'Morty' Smith"}
db.child("users").child("Morty").set(data)

My database rules are set to:

{
 "rules": {
".read": "auth != null",
".write": "auth != null"
}
}
0

2 Answers 2

13

I ran into this same issue trying to upload data to the database. I reread the beginning of: https://github.com/thisbejim/Pyrebase/blob/master/README.md

import pyrebase

config = {
  "apiKey": "apiKey",
  "authDomain": "projectId.firebaseapp.com",
  "databaseURL": "https://databaseName.firebaseio.com",
  "storageBucket": "projectId.appspot.com",
  "serviceAccount": "path/to/serviceAccountCredentials.json"
}

firebase = pyrebase.initialize_app(config)

Add the serviceAccount entry into the configuration with a path to a key that you can download from Firebase.

enter image description here

To get there: Settings > Project Settings > Service Accounts > Generate New Private Key.

Put that key in some desired location and put that location in the "serviceAccount" path.

Hope it helps.

2
  • 2
    Didn't think the serviceAccount was necessary when we've successfully signed in but... This worked!
    – Vitor
    Aug 19, 2018 at 16:52
  • 1
    @Vitor I would advice not to add a serviceAccount, this skips all security rules you have set. Rather, pass the user['idToken'] as Jose indicated in their answer. See also github.com/thisbejim/Pyrebase/blob/master/… for more detail Mar 16, 2020 at 15:44
2

set method is missing the user['idToken'], and you forgot to authenticate, try:

import pyrebase
config = {
"apiKey": "*****",
"authDomain": "***-bot.firebaseapp.com",
"databaseURL": "https://***-bot.firebaseio.com",
"storageBucket": "ebo-bot.appspot.com"
}

firebase = pyrebase.initialize_app(config)
db = firebase.database()
auth = firebase.auth()

user = auth.sign_in_with_email_and_password("[email protected]", "passwordhere")

data = {"name": "Mortimer 'Morty' Smith"}
db.child("users").child("Morty").set(data,user['idToken'])

(you also need to create an user before running this, go to your firebase dashboard and click on the authentication tab, you can add users there)

1

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.