繁体   English   中英

如何使用 Python 验证到 Firebase?

[英]How to authenticate to Firebase using Python?

我正在使用 Python 构建一个 Web 应用程序,我想对用户进行身份验证。 pyrebase package 似乎已过时,它会产生依赖性错误,因此我无法使用它。 我知道有一个来自 firebase firebase-admin API 的 function 是这样工作的:

from firebase import auth
email = example@example.com
user = auth.get_user_by_email(email)

但是如果这个用户有密码怎么办? 我想检查 email 和密码是否都正确提供。 提前致谢。

Firebase Admin SDK 没有当前用户的概念,因此没有 API 可以根据用户的凭据“登录”用户。

Since you're building a web app, the usual flow is to use the Firebase JavaScript SDK in your client-side code to sign the user in . 如果需要,您可以将 ID 令牌从客户端发送到服务器上的 Python 代码,并在那里执行基于用户的操作。

Firebase 管理员 SDK 不提供 API 来通过密码验证和/或验证用户。

然而,Firebase为此提供了Firebase Auth REST API。 要使用 REST API,您需要从 Firebase 控制台获取您的 Web API 密钥。

找到 Web API 密钥

从 Firebase 控制台导航到项目设置,然后在常规选项卡上找到 Web API 密钥。 Web Api 密钥是在您将应用程序添加到 firebase 项目时自动生成的。 您还可以 go 到应用程序设置以从firebaseConfig中查找apiKey

实施认证

假设你想实现用户登录

def sign_in_with_email_and_password(email, password, return_secure_token=True):
    payload = json.dumps({"email":email, "password":password, "return_secure_token":return_secure_token})
    FIREBASE_WEB_API_KEY = 'the web API key here' 
    rest_api_url = "https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword"

    r = requests.post(rest_api_url,
                  params={"key": FIREBASE_WEB_API_KEY},
                  data=payload)

    return r.json()

参考

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM