Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I can read but not create #1

Open
JMFUZ opened this issue Nov 1, 2022 · 3 comments
Open

I can read but not create #1

JMFUZ opened this issue Nov 1, 2022 · 3 comments

Comments

@JMFUZ
Copy link

JMFUZ commented Nov 1, 2022

Hi,
I'm trying this library and fth firebase-firestore one.
I can connect to my database because I can read
But I can't write in it and I've no error message, then I can't know why

Firestore rules :

rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /users/{user} {
allow read, write: if request.auth != null;
}
}
}

My code :

import os
import network
import time
import ujson, urequests, _thread

SSID = "SSID"
password = "password"
wlan = network.WLAN(network.STA_IF)
wlan.active(True)

if not wlan.isconnected():
wlan.connect(SSID, password)
print("Attente de la connexion WIFI", end="...")
while not wlan.isconnected():
print(".", end="")
time.sleep(1)
print()

print("Reseau WIFI", SSID , "connecté :", wlan.ifconfig()[0])

import ufirestore as firestore
from json import FirebaseJson
from firebase_auth import FirebaseAuth
from firebase_auth import AuthSession

firestore.set_project_id("firestore-db-test1")

auth = FirebaseAuth("API_KEY")
auth.sign_in("[email protected]", "AAAAAAA")

firestore.set_access_token(auth.session.access_token)

print("===== Lecture =====")
print("")
raw_doc = firestore.get("users/")
print("raw_doc : ", raw_doc)
print("")

print("===== Ecriture =====")
print("")
doc = FirebaseJson()
doc.set("Name/stringValue", "John Doe")
doc.set("Age/integerValue", 25)

response = firestore.create("users/", doc, bg=False)
print(response)

auth.sign_out()

The results :

===== Lecture =====

raw_doc : {'documents': [{'fields': {'Age': {'stringValue': '21'}, 'Name': {'stringValue': 'Jane Doe'}}, 'createTime': '2022-10-31T12:32:34.756017Z', 'name': 'projects/firestore-db-test1/databases/(default)/documents/users/FhjDBdURqcaQlpwdXlFG', 'updateTime': '2022-10-31T12:32:34.756017Z'}]}

===== Ecriture =====

Traceback (most recent call last):
File "", line 92, in
File "ufirestore.py", line 222, in create
File "ufirestore.py", line 90, in create
File "ufirestore.py", line 53, in send_request
File "urequests.py", line 104, in request
AssertionError:

I need help because I really don't know I happened

Thanks in advance

@JMFUZ
Copy link
Author

JMFUZ commented Jun 22, 2023 via email

@rhylkiio
Copy link

Hello, I found my problem wasn't with this firebase-auth library but with micropython-firebase-firestore. I posted in the wrong forum.
My create method still has an issue I can't create a document on Firestore but I have been able to read documents.I am trying to implement some of the solutions in this thread: WoolDoughnut310/micropython-firebase-firestore#5
Please let me know if you succeed in creating a document.
To solve your issue remove the "params argument" in send request. Here is my updated send request code:

def send_request(path, method="get", params=dict(), data=None, dump=True):
    headers = {}

    if FIREBASE_GLOBAL_VAR.ACCESS_TOKEN:
        headers["Authorization"] = "Bearer " + FIREBASE_GLOBAL_VAR.ACCESS_TOKEN

    response = urequests.request(
        method, path,headers=headers, json=data) #removed params argument

    if dump == True:
        if response.status_code < 200 or response.status_code > 299:
            print(response.text)
            raise FirestoreException(response.reason, response.status_code)

        json = response.json()
        if json.get("error"):
            error = json["error"]
            code = error["code"]
            message = error["message"]
            raise FirestoreException(message, code)
        return json
       

@rhylkiio
Copy link

rhylkiio commented Jun 22, 2023

Hello, I found the problem, to solve Modify your json.py def process function by commenting out the name. Here is the updated code :

def process(self, name):
        return {
           # "name": name,
            "fields": self.data
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants