Python 当我试图打印登录用户的本地化ID时,它不会';行不通
我终于找到了如何从firebase类访问localId实例,但现在当用户登录并尝试打印localId时,对象返回为none。当我尝试在firebase类中打印它时,它会返回值,但在类之外它不起作用。这是我的firebase课程Python 当我试图打印登录用户的本地化ID时,它不会';行不通,python,firebase,firebase-authentication,kivy,Python,Firebase,Firebase Authentication,Kivy,我终于找到了如何从firebase类访问localId实例,但现在当用户登录并尝试打印localId时,对象返回为none。当我尝试在firebase类中打印它时,它会返回值,但在类之外它不起作用。这是我的firebase课程 class MyFireBase(): def __init__(self): # initialize localId to None, just to be sure it always exists self.localId
class MyFireBase():
def __init__(self):
# initialize localId to None, just to be sure it always exists
self.localId = None
def sign_up(self, email, password):
app = App.get_running_app()
email = email.replace("\n","")
password = password.replace("\n","")
# Send email and password to Firebase
# Firebase will return localId, authToken (idToken), refreshToken
signup_url = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?key=" + self.wak
signup_payload = {"email": email, "password": password, "returnSecureToken": True}
sign_up_request = requests.post(signup_url, data=signup_payload)
sign_up_data = json.loads(sign_up_request.content.decode())
print(sign_up_request.ok)
print(sign_up_request.content.decode())
if sign_up_request.ok == True:
print(sign_up_data)
refresh_token = sign_up_data['refreshToken']
self.localId = sign_up_data['localId']
idToken = sign_up_data['idToken']
# Save refreshToken to a file
with open(app.refresh_token_file, "w") as f:
f.write(refresh_token)
app.local_id = self.localId
app.id_token = idToken
my_data = '{"avatar": "profilepic.png", "jobs_done": "", "jobs_posted": ""}'
post_request = requests.patch("https://moonlighting-bb8ab.firebaseio.com/users/" + self.localId + ".json?auth=" + idToken, data=my_data)
print(post_request.ok)
print(post_request.content.decode())
app.root.current = "create"
elif sign_up_request.ok == False:
error_data = json.loads(sign_up_request.content.decode())
error_message = error_data["error"]['message']
app.root.ids.signup.ids.signup_message.text = error_message.replace("_", " ")
def send_user_details(self):
app = App.get_running_app()
my_data ={"first name": app.root.ids.create.ids.first_name.text, "last name": app.root.ids.create.ids.last_name.text,
"phone number": app.root.ids.create.ids.phone_number.text, "job1": app.root.ids.create.ids.job1.text, "job2": app.root.ids.create.ids.job2.text,
"job3": app.root.ids.create.ids.job3.text, "date of birth": app.root.ids.create.ids.date_of_birth.text, "state": app.root.ids.create.ids.state1.text}
user_details = requests.patch("https://moonlighting-bb8ab.firebaseio.com/users/" + app.local_id + ".json?auth=" + app.id_token, json.dumps(my_data))
print(user_details.ok)
print(user_details.content.decode())
app.root.current = "main"
def sign_in_existing_user(self, email, password):
signin_url = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key=" + self.wak
signin_payload = {"email": email, "password": password, "returnSecureToken": True}
signin_request = requests.post(signin_url, data=signin_payload)
sign_up_data = json.loads(signin_request.content.decode())
app = App.get_running_app()
print(signin_request.ok)
print(signin_request.content.decode())
if signin_request.ok == True:
refresh_token = sign_up_data['refreshToken']
self.localId = sign_up_data['localId']
idToken = sign_up_data['idToken']
# Save refreshToken to a file
with open(app.refresh_token_file, "w") as f:
f.write(refresh_token)
# Save localId to a variable in main app class
# Save idToken to a variable in main app class
app.local_id = self.localId
app.id_token = idToken
# Create new key in database from localId
# Get friend ID
# Get request on firebase to get the next friend id
# --- User exists so i dont need to get a friend id
# self.friend_get_req = UrlRequest("https://friendly-fitness.firebaseio.com/next_friend_id.json?auth=" + idToken, on_success=self.on_friend_get_req_ok)
# app.change_screen("home_screen")
app.root.current = "main"
elif signin_request.ok == False:
error_data = json.loads(signin_request.content.decode())
error_message = error_data["error"]['message']
app.root.ids.login.ids.login_message.text = error_message.replace("_", " ")
这是我试图打印它的班级
class ProfileWindow(Screen):
def on_enter(self, *args):
print(MyFireBase().localId)
如能提供任何帮助,我们将不胜感激
下面是ProfileWindow的代码
class ProfileWindow(Screen):
def __int__(self, thefirebase, *args, **kwargs):
super().__init__(*args, **kwargs)
self.thefirebase = thefirebase
def on_enter(self, *args):
print(self.thefirebase.localId)
我将其重命名为firebase,因为我有一个模块,我也在使用它,名为firebase 调用
sign\u up()
方法时,可以在MyFireBase
实例上设置localId
变量。这意味着,如果要检索localId
,则需要使用相同的MyFireBase
实例。在enter上创建一个全新的MyFireBase
实例,只需返回localId
的初始值,即None
,因为该实例上没有调用sign\u()
您需要重新构造代码,以便在创建ProfileWindow
时,将初始化的MyFireBase
实例传递给它
例如:
class ProfileWindow(Screen):
def __init__(self, firebase, **kwargs):
super().__init__(**kwargs)
self.firebase = firebase # Store the initialised MyFireBase instance
def on_enter(self, *args):
print(self.firebase.localId) # Use the initialised instance
然后在创建时将MyFireBase
实例传递到ProfileWindow
firebase = MyFireBase()
firebase.sign_up(...)
...
window = ProfileWindow(firebase) # Pass the initialised MyFireBase instance
调用
sign\u()
方法时,可以在MyFireBase
实例上设置localId
变量。这意味着,如果要检索localId
,则需要使用相同的MyFireBase
实例。在enter上创建一个全新的MyFireBase
实例,只需返回localId
的初始值,即None
,因为该实例上没有调用sign\u()
。谢谢,那么您建议我怎么做呢?您需要重新构造代码,以便在实例化时将用于注册的相同的MyFireBase
实例传递给ProfileWindow
类。然后,enter上的方法可以引用该初始化实例。你能发布创建ProfileWindow
的代码吗?我可以举个例子。我希望举个例子。我对这行代码有问题window=ProfileWindow(firebase)
im收到这个错误代码TypeError:\uu init\uuu()接受1个位置参数,但给出了2个
你能为ProfileWindow
类发布你的代码吗(特别是\uuuuuu init\uuuuu
方法)?我编辑了问题并添加了它,感谢你的帮助从\uuu init\uuuuu()中删除*args
)
-我已经更新了我的答案以进行说明。你能发布完整的回溯吗?