当我进行授权时,请使用以下命令调用Firebase:
email = request.POST['email']
password = request.POST['password']
config = {
"apiKey": "",
"authDomain": "",
"databaseURL": "",
"storageBucket": ""
}
firebase = pyrebase.initialize_app(config)
auth = firebase.auth()
try:
user = auth.create_user_with_email_and_password(email, password)
user = auth.refresh(user['refreshToken'])
request.session['user_uuid'] = user
if user != "":
print("You're logged in.")
return redirect('')
else:
print("")
except requests.exceptions.HTTPError as e:
print(e)
我收到此回复:
"[Errno 400 Client Error: Bad Request for url:
https://www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?
key=%20AIzaSyCm0XR9oECpQOBTp_iouf3RJTD-Nq0fuXg] {
"error": {
"errors": [
{
"domain": "global",
"reason": "invalid",
"message": "WEAK_PASSWORD : Password should be at least 6 characters"
}
],
"code": 400,
"message": "WEAK_PASSWORD : Password should be at least 6 characters"
}
}"
我试图解析响应中的消息和代码,但我一直收到可订阅的错误。我使用以下代码来解析响应:
data = e[-1]
data = (json.dumps(json.loads(data), sort_keys = True, indent = 4))
data = json.loads(data)
error = data['error']
error_message = error['message']
print(error_message)
context_dict = {
'message': error_message
}
答案 0 :(得分:0)
pyrebase
引发了更新requests.HTTPError
exception;重新包装原始异常。
您可以使用.args
属性获取该异常包含的两个值。它是原始异常的元组,第二个是request_object.text
值。这是您要解析的第二个值,或您可以使用原始异常来获取响应对象,并使用.json()
让请求库进行解析。
except requests.exceptions.HTTPError as e:
error_json = e.args[1]
error = json.loads(error_json)['error']
或
except requests.exceptions.HTTPError as e:
response = e.args[0].response
error = response.json()['error']
说实话,我不太清楚为什么这个项目会这样做;原始异常已包含他们捆绑的信息;例如而不是重新包装异常,使用original_exception.response.text
生成相同的文本值。