I'm trying to connect to my realtime database from firebase using MicroPython in a ESP32, I was able to retrieve the data using ufirebase but only if the rules were public for reading and writing, now I have the following rules for the database:
{
"rules": {
".read": "auth.uid === <My uid>",
".write": "auth.uid === <My uid>"
}
}
I am able to get my uid from my email and password that were registered in the authentication section in firebase using the micropython-firebase-auth library, but now I am not able of doing the request using ufirebase, I have tried doing a request with urequests in the following way:
import urequests
def get_data():
response = urequests.get("https://<DATABASE_NAME>.firebaseio.com/<NODE>.json?auth=<My uid>")
data = response.json()
return data
But doing so, shows me the following message: {'error': 'Permission denied'}
So I haven't been able of retrieving the information with the applied set of rules, the structure of the data is the following for all of the database.
Example of data to retrieve from the realtime database
Does anyone know how to do a request with micropython once you got the uid?
You can't authentication by just passing your UID to the database. That'd be a huge security risk, as the UID is not a secret. You'll instead need to pass the user's ID token, which amongst others includes their UID, as shown in the documentation on authenticating REST requests.
If your microcontroller runs in a trusted environment, consider using the Admin SDK as shown here: How to authenticate to Firebase using Python?.
If the device should be considered untrusted, there is no Firebase SDK for client-side Python to generate such a token though. Your options in that case are to either call the REST API to authenticate the user, or use a 3rd party wrapper such as Pyrebase to sign in