Zvi*_*arp 6 python python-2.7 firebase firebase-authentication
我正在尝试从python(2.7)处理firebase上的数据.
这是我的规则(在firebseio.com上):
{
"rules": {
"user": {
"$uid": {
".read": "auth != null && auth.uid == $uid",
".write": "auth != null && auth.uid == $uid"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的数据库的截图:
最后,我的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
Run Code Online (Sandbox Code Playgroud)
现在我无法弄清楚如何获取数据并从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',))
Run Code Online (Sandbox Code Playgroud)
任何人都可以为我提供工作代码吗?
提前致谢,
Zvi Karp
以下是我们为使身份验证工作而采取的步骤.
(1)首先你需要一个Firebase秘密.在Firebase中安装项目后,单击"设置".然后单击"数据库"并选择创建密码. 
复制你的秘密.它将在稍后进入您的代码.
(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
Run Code Online (Sandbox Code Playgroud)
(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/'
Run Code Online (Sandbox Code Playgroud)
(5)将此代码添加到MainHandler(假设您使用的是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 />')
Run Code Online (Sandbox Code Playgroud)
现在,当您导航到Firebase实时数据库时,您应该看到Harry Potter作为用户和其他人的条目.
| 归档时间: |
|
| 查看次数: |
10344 次 |
| 最近记录: |