简体   繁体   English

错误默认的 Firebase 应用已经存在。 这意味着您多次调用 initialize_app() 而没有提供应用程序名称作为

[英]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.

相关问题 '默认的 Firebase 应用不存在。 确保通过调用 initialize_app()' 初始化 SDK - 'The default Firebase app does not exist. Make sure to initialize the SDK by calling initialize_app()' Google Cloud Functions Firebase 错误 默认的 Firebase 应用已经存在 - Google Cloud Functions Firebase Error The default Firebase app already exists Firebase 名为“[DEFAULT]”的应用已存在 REACT - Firebase App named '[DEFAULT]' already exists REACT Firebase 名为“[DEFAULT]”的 App 已经存在 - A Firebase App named "[DEFAULT]" already exists 没有 Firebase 应用程序“[默认]”错误,但 firebase 已经初始化 - No Firebase App '[Default]' error but firebase is already initialized 您已经拥有一个名称为 package 的应用程序 - you already have an app with that package name 你应该在哪里初始化你的 firebase web 应用程序? - Where are you supposed to initialize your firebase web app? 使用 Firebase 管理员 SDK,无法部署 Firebase 跨多个文件拆分的云功能:“默认 Firebase 应用程序已存在” - Using Firebase Admin SDK, Unable to Deploy Firebase Cloud Functions Split Across Multiple Files: "The default Firebase app already exists" ReactNative 在应用程序启动时初始化 Firebase? - ReactNative initialize Firebase at app start? AngularFirestore 已经在 [DEFAULT] Firebase 应用程序上用不同的设置初始化 - AngularFirestore was already initialized on the [DEFAULT] Firebase App with different settings
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM