[英]ERROR The default Firebase app already exists. This means you called initialize_app() more than once without providing an app name as the
i try to connnect at my firebase application and i have an error我尝试连接我的 firebase 应用程序,但出现错误
This is my code:这是我的代码:
@event_routes.route('/scanner/events', methods=['GET'], cors=True)
def get_scanner_events() -> Response:
try:
id_token = event_routes.current_request.headers["IdToken"]
except KeyError:
return Response(body={"message": "missing id token for authentication"}, status_code=400,
headers={"Content-Type": "application/json"})
# add json content (dict)
try:
GOOGLE_APPLICATION_CREDENTIALS = json.loads(os.getenv("GOOGLE_APPLICATION_CREDENTIALS", ""))
TEST_GOOGLE_PRIVATE_KEY = os.getenv("TEST_GOOGLE_PRIVATE_KEY", "").replace(r'\n', '\n')
GOOGLE_APPLICATION_CREDENTIALS['private_key'] = TEST_GOOGLE_PRIVATE_KEY
cred = firebase_admin.credentials.Certificate(GOOGLE_APPLICATION_CREDENTIALS)
firebase_app = firebase_admin.initialize_app(credential=cred)
value = firebase_admin.auth.verify_id_token(id_token)
# print("made it here")
print(firebase_app.project_id)
except auth.InvalidIdTokenError as e:
print("InvalidIdTokenError")
return Response(body={"message": "invalid id token"}, status_code=400,
headers={"Content-Type": "application/json"})
this is my error:这是我的错误:
ERROR The default Firebase app already exists. This means you called initialize_app() more than once without providing an app name as the second argument. In most cases you only need to call initialize_app() once. But if you do want to initialize multiple apps, pass a second argument to initialize_app() to give each app a unique name.
I checked on Inte.net but i am quite lost.我在 Inte.net 上查过,但我完全迷路了。
Thanks for yours answers!感谢您的回答!
As the error says, you need to initialize Firebase only once.如错误所述,您只需初始化 Firebase 一次。
firebase_app = firebase_admin.initialize_app(credential=cred)
This statement runs every time /scanner/events
route is called.每次调用
/scanner/events
路由时都会运行此语句。 Instead you should move this statement out of any route and initialize only once at the top.相反,您应该将此语句移出任何路由并仅在顶部初始化一次。 For example:
例如:
# Define this at the top
GOOGLE_APPLICATION_CREDENTIALS = json.loads(os.getenv("GOOGLE_APPLICATION_CREDENTIALS", ""))
TEST_GOOGLE_PRIVATE_KEY = os.getenv("TEST_GOOGLE_PRIVATE_KEY", "").replace(r'\n', '\n')
GOOGLE_APPLICATION_CREDENTIALS['private_key'] = TEST_GOOGLE_PRIVATE_KEY
cred = firebase_admin.credentials.Certificate(GOOGLE_APPLICATION_CREDENTIALS)
# This must run only once
firebase_app = firebase_admin.initialize_app(credential=cred)
@event_routes.route('/scanner/events', methods=['GET'], cors=True)
def get_scanner_events() -> Response:
try:
id_token = event_routes.current_request.headers["IdToken"]
except KeyError:
return Response(body={"message": "missing id token for authentication"}, status_code=400,
headers={"Content-Type": "application/json"})
# add json content (dict)
try:
value = firebase_admin.auth.verify_id_token(id_token)
# print("made it here")
print(firebase_app.project_id)
except auth.InvalidIdTokenError as e:
print("InvalidIdTokenError")
return Response(body={"message": "invalid id token"}, status_code=400,
headers={"Content-Type": "application/json"})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.