1

I am trying to save data in my firebase realtime database using this rule:

{
  "rules": {
    "users": {
      "$uid": {
        // Allow only authenticated content owners access to their data
        ".read": "auth != null && auth.uid == $uid",
        ".write": "auth != null && auth.uid == $uid",
      },
    },
  },
}

I need to create a child of users and name it the uid of that user. So, I did this:

user=auth.sign_in_with_email_and_password('email', 'password')
database.child('users').child(user['localId']).set("nahid",user['localId'])

but when I run this, I get this error:

requests.exceptions.HTTPError: [Errno 401 Client Error: Unauthorized for url: https://khelobd-6f730-default-rtdb.firebaseio.com/users/7Km7srPfqqccZwa0On5i2QaJDUi2.json?auth=7Km7srPfqqccZwa0On5i2QaJDUi2] {
  "error" : "Could not parse auth token."
}

How can I solve this?

Nahid Khan
  • 11
  • 1

1 Answers1

1

The ?auth= param does not seems to be user's Firebase ID Token but the UID itself. Try changing user['localId'] in set() to user['idToken']:

user=auth.sign_in_with_email_and_password('email', 'password')
database.child('users').child(user['localId']).set("nahid",user['idToken'])
#                                                                  ^^^
Dharmaraj
  • 47,845
  • 8
  • 52
  • 84