代码之家  ›  专栏  ›  技术社区  ›  beano

使用存储在secret Manager中的机密初始化Firebase管理SDK

  •  0
  • beano  · 技术社区  · 4 年前

    我尝试在云运行应用程序中初始化Firebase管理SDK,使用单独的服务帐户(即不是默认服务帐户)。

    文件表明:

    import firebase_admin
    from firebase_admin import credentials
    
    cred = credentials.Certificate("path/to/serviceAccountKey.json")
    firebase_admin.initialize_app(cred)
    

    import firebase_admin
    from google.cloud import secretmanager
    from google.oauth2 import service_account
    
    # Create credentials object then initialize the firebase admin client
    sec_client = secretmanager.SecretManagerServiceClient()
    name = sec_client.secret_version_path(GOOGLE_CLOUD_PROJECT_NUMBER, FIREBASE_SA_SECRET_NAME, "latest")
    response = sec_client.access_secret_version(name)
    service_account_info = json.loads(response.payload.data.decode('UTF-8'))
    creds = service_account.Credentials.from_service_account_info(service_account_info)
    firebase_admin.initialize_app(creds)
    

    收到错误:

    ValueError:提供了非法的Firebase凭据。应用程序必须 使用有效的凭据实例初始化。

    如有任何提示,不胜感激。

    0 回复  |  直到 4 年前
        1
  •  1
  •   beano    4 年前
    import firebase_admin
    from google.cloud import secretmanager
    from google.oauth2 import service_account
    
    # Create credentials object then initialize the firebase admin client
    sec_client = secretmanager.SecretManagerServiceClient()
    name = sec_client.secret_version_path(GOOGLE_CLOUD_PROJECT_NUMBER, FIREBASE_SA_SECRET_NAME, "latest")
    response = sec_client.access_secret_version(name)
    service_account_info = json.loads(response.payload.data.decode('utf-8'))
    
    # build credentials with the service account dict
    creds = firebase_admin.credentials.Certificate(service_account_info)
    
    # initialize firebase admin
    firebase_app = firebase_admin.initialize_app(creds)
    
        2
  •  0
  •   Diego Rodríguez    4 年前

    initialize_app 文件:

      credential: A credential object used to initialize the SDK (optional). If none is provided,
          Google Application Default Credentials are used.
    

    https://github.com/firebase/firebase-admin-python/blob/b5f228f68d95a4c24efa39e47e30045047db1a6e/firebase_admin/__init__.py#L48-L49

    所以,你可以打电话

    firebase_admin.initialize_app()