python中的Firebase用户认证

2024-04-26 14:32:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从python(2.7)获取firebase上的数据。

以下是我的规则(在firebseio.com上):

{
    "rules": {
        "user": {
          "$uid": {
            ".read": "auth != null && auth.uid == $uid",
            ".write": "auth != null && auth.uid == $uid"
          }
        }
    }
}

以下是我的数据库截图:

enter image description here

最后,我的python代码:

from firebase import firebase
from firebase.firebase import FirebaseApplication, FirebaseAuthentication

DSN = 'https://<my name>.localhost'
EMAIL = 'qqq@qqq.qqq'
authentication = FirebaseAuthentication(EMAIL, True, True, extra={'id': '<the user id>'})
firebase = FirebaseApplication(DSN, authentication)
firebase.authentication = authentication
print authentication.extra

user = authentication.get_user()
print user.firebase_auth_token

现在我不知道如何获取数据,以及如何向firebase发送数据。 我试图使用行:result = firebase.get('/users', None, {'print': 'pretty'}),但它给出了这个错误:

ConnectionError: HTTPSConnectionPool(host='<my name>.localhost', port=443): Max retries exceeded with url: /users/.json?print=pretty&auth=<the token code of the user> (Caused by NewConnectionError('<requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x02A913B0>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed',))

有人能给我一个工作代码吗?

提前谢谢你

兹维卡普


Tags: the代码fromimportauthuidauthenticationnull
1条回答
网友
1楼 · 发布于 2024-04-26 14:32:42

下面是我们为使身份验证工作而采取的步骤。

(1)首先你需要一个Firebase秘密。 在Firebase中创建项目后,单击“设置”。然后单击“数据库”并选择创建机密。settings

复制你的秘密。稍后它将进入您的代码。

secret

(2)您需要您的firebase URL。 它的格式如下:https://.firebaseio.com 也复制这个。

(3)为Python获取Firebase REST API。 我们用了这个:https://github.com/benletchford/python-firebase-gae 导航到lib目录上方并运行此命令,将firebase代码放入lib目录:

git clone http://github.com/benletchford/python-firebase-gae lib/firebase

(4)在您的“main.py”文件(或您正在使用的任何文件)中添加以下代码:

from google.appengine.ext import vendor
vendor.add('lib')

from firebase.wrapper import Firebase

FIREBASE_SECRET = 'YOUR SECRET FROM PREVIOUS STEPS'
FIREBASE_URL = 'https://[…].firebaseio.com/'

(5)将此代码添加到主处理程序(假设您正在使用AppEngine):

class MainHandler(webapp2.RequestHandler):
    def get(self):
        fb = Firebase(FIREBASE_URL + 'users.json', FIREBASE_SECRET)

        new_user_key = fb.post({
            "job_title": "web developer",
            "name": "john smith",
        })
        self.response.write(new_user_key)
        self.response.write('<br />')

        new_user_key = fb.post({
            "job_title": "wizard",
            "name": "harry potter",
        })
        self.response.write(new_user_key)
        self.response.write('<br />')

        fb = Firebase(FIREBASE_URL + 'users/%s.json' % (new_user_key['name'], ), FIREBASE_SECRET)
        fb.patch({
            "job_title": "super wizard",
            "foo": "bar",
        })

        fb = Firebase(FIREBASE_URL + 'users.json', FIREBASE_SECRET)
        self.response.write(fb.get())
        self.response.write('<br />')

现在,当您导航到您的Firebase实时数据库时,您应该可以看到Harry Potter作为用户和其他用户的条目。

相关问题 更多 >