【问题标题】:How to use Firebase REST API(Python) for authentication of a user?如何使用 Firebase REST API(Python) 对用户进行身份验证?
【发布时间】:2016-10-01 02:51:48
【问题描述】:

我正在使用 Firebase 作为我的物联网应用的后端。在我的 Raspberry Pi 上使用 python,我将数据存储在 Firebase 上。截至目前,我的数据库对 r/w 是公开的。我使用 Console>Auth>Users 创建了一个虚拟帐户。我想使用此帐户通过我的 Raspberry Pi 使用带有 REST 或任何库的 Python 登录我的 Firebase 应用程序。

登录后,我将使用“数据库>规则”来限制用户只能读/写他的数据。我是 Firebase 的新手! Firebase 也弃用旧文档。我想知道如何继续我的应用程序。我找不到任何有关如何在 Python 或 REST API 上执行此操作的信息。

https://firebase.google.com/docs/auth/ 这是我找到的用于身份验证的新文档,其中不包含使用 REST API 的信息。

有没有办法实现我的愿望?任何帮助表示赞赏。

【问题讨论】:

    标签: python firebase firebase-authentication


    【解决方案1】:

    来自 Firebase 身份验证团队的 Alfonso。

    我们目前没有用于身份验证的受支持或记录的 REST API。

    您仍然可以通过旧版控制台创建项目并使用旧版文档来学习如何创建您自己的自定义身份验证令牌,并将它们发送到数据库 REST API 进行身份验证。

    【讨论】:

    • 谢谢! :) 因为我需要 Firebase Cloud Messaging,所以我选择了更新的 Firebase :) 是否正在为新的 Firebase 编写/更新 REST API?如果你被允许对此发表评论。 :)
    • 嘘!请为 Firebase 中的身份验证创建一个受支持的 REST API。
    • 我正在将所有这些反馈传递给团队 :) @Ryan:对于云消息传递,有一个 REST API 用于触发通知;这是你需要的吗?
    • 查看 stackoverflow.com/questions/37408684/… 了解对 Firebase 数据库进行经过身份验证的 REST 调用的方法
    • @AlfonsoGomezJordanaManas 您可以说旧版自定义身份验证令牌将支持多长时间?当前的控制台说它们已被弃用。
    【解决方案2】:

    我最近遇到了这个问题,出于我的目的,我最终为 firebase_admin 制作了一个猴子补丁,它在主库中添加了一个 verify_user 方法。 (我也放在pypi上,https://pypi.org/project/firebase_fave/pip install firebase_fave

    # import several different ways to make the code read like it would as part of the package it belongs in
    import firebase_admin
    from firebase_admin._user_mgt import *
    from firebase_admin.auth import _get_auth_service, AuthError
    from firebase_admin import firestore
    
    
    # helper to add methods
    def _add_method(cls):
        def cls_decorator(func):
            @wraps(func)
            def copied(self, *args, **kwargs): # wrap so we don't bind the func
                return func(self, *args, **kwargs)
            setattr(cls, func.__name__, copied)
            return func
        return cls_decorator
    
    
    # add password verify to user manager
    @_add_method(firebase_admin._user_mgt.UserManager)
    def verify_user(self, **kwargs):
        """Gets the user data corresponding to the provided data and verifies"""
        key, key_type = self.get_user(**kwargs)['email'], 'email'
    
        if 'password' in kwargs:
            password = kwargs.pop('password')
        else:
            password = ''
    
        payload = {key_type:key, 'password':password, "returnSecureToken": True}
    
        try:
            response = self._client.request('post', 'verifyPassword', json=payload)
        except requests.exceptions.RequestException as error:
            msg = 'Failed to get user by {0}: {1}.'.format(key_type, key)
            self._handle_http_error(INTERNAL_ERROR, msg, error)
        else:
            if not response:
                raise ApiCallError(
                    USER_NOT_FOUND_ERROR,
                    'No user record found for the provided {0}: {1}.'.format(key_type, key))
            return response
    
    
    # as in firebase_admin, we want a convenience method as well
    def _outer_verify_user(password, **kwargs):
        """Verifies a user given password and one of uid or email.
        Args:
            uid: A user ID string.
            email: user e-mail address.
            app: An App instance (optional).
    
        Returns:
            UserRecord: A UserRecord instance.
    
        Raises:
            ValueError: if both user ID and email are None, empty, or malformed
            AuthError: If an error occurs while deleting the user account.
        """
        app = kwargs.pop('app', None)
        user_manager = _get_auth_service(app).user_manager
        kwargs['password'] = password
        try:
            return user_manager.verify_user(**kwargs)
        except firebase_admin._user_mgt.ApiCallError as error:
            raise AuthError(error.code, str(error), error.detail)
    
    
    # finally, apply this convenience method to the class.
    firebase_admin.verify_user = _outer_verify_user
    

    【讨论】:

      猜你喜欢
      • 2018-09-11
      • 1970-01-01
      • 2012-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多