You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Python版Firebase Admin SDK验证Firebase安装ID是否存在?

验证Firebase安装ID的正确方法

首先明确:Firebase安装ID(比如示例中的ezOi0OrW6UQAuyf9m0MeRq)和Firebase Auth的用户ID是完全独立的标识体系,你之前调用的用户相关函数是针对Auth用户的,自然会返回“用户ID未找到”——这是因为操作对象完全不对。

Firebase Admin SDK本身没有直接封装验证安装ID的方法,需要通过Firebase Installations REST API来实现验证,以下是具体步骤和Python代码示例:

实现步骤

  1. 利用Firebase Admin SDK生成有权限访问Installations服务的OAuth2访问令牌
  2. 向Installations服务的REST端点发送GET请求,传入项目ID和待验证的安装ID
  3. 根据HTTP响应状态判断ID是否存在:200表示存在,404表示不存在

Python代码示例

import requests
import firebase_admin
from firebase_admin import credentials

# 初始化Admin SDK(确保你的服务账号密钥路径正确)
cred = credentials.Certificate("path/to/serviceAccountKey.json")
firebase_admin.initialize_app(cred)

def verify_firebase_installation_id(project_id, installation_id):
    # 获取访问令牌
    token = cred.get_access_token().access_token
    url = f"https://firebaseinstallations.googleapis.com/v1/projects/{project_id}/installations/{installation_id}"
    
    headers = {
        "Authorization": f"Bearer {token}",
        "Content-Type": "application/json"
    }
    
    response = requests.get(url, headers=headers)
    if response.status_code == 200:
        return True, response.json()  # 返回存在及安装ID的详细信息
    elif response.status_code == 404:
        return False, "安装ID不存在"
    else:
        return False, f"请求失败:{response.status_code} - {response.text}"

# 调用示例
project_id = "your-firebase-project-id"
installation_id = "ezOi0OrW6UQAuyf9m0MeRq"
exists, result = verify_firebase_installation_id(project_id, installation_id)
print(exists, result)

注意事项

  • 确保你的服务账号拥有firebaseinstallations.installations.get权限(默认的Editor角色已包含该权限)
  • 如果返回403错误,需要检查服务账号的权限配置
  • 安装ID的有效期由客户端管理,过期的ID也会返回404

内容的提问来源于stack exchange,提问作者Tbonepgh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 05:15:29