2016-04-03 32 views
5

Tôi đang cố gắng acsess dữ liệu trên firebase từ python (2.7).Xác thực người dùng Firebase trong python

Dưới đây là quy tắc của tôi (trên firebseio.com):

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

Đây là một ảnh chụp màn hình của databse của tôi:

enter image description here

Và cuối cùng, mã python của tôi:

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

DSN = 'https://<my name>.localhost' 
EMAIL = '[email protected]' 
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 

Bây giờ tôi không thể tìm cách lấy dữ liệu và gửi dữ liệu đến và đi từ firebase. Tôi tryed useing dòng: result = firebase.get('/users', None, {'print': 'pretty'}), Nhưng nó mang lại cho tôi lỗi này:

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',)) 

Bất cứ ai cũng có thể cung cấp cho tôi với một mã làm việc?

Cảm ơn trước,

Zvi Karp

+0

Nếu Im không sử dụng DSN thì sao? – marciokoko

Trả lời

6

Sau đây là các bước mà chúng tôi đã trải qua thẩm định làm việc.

(1) Trước tiên, bạn cần có Bí mật Firebase. Sau khi bạn có dự án trong Firebase, sau đó nhấp vào Cài đặt. Sau đó nhấp vào Cơ sở dữ liệu và chọn tạo bí mật. settings

Sao chép bí mật của bạn. Nó sẽ đi vào mã của bạn sau.

secret

(2) Bạn cần URL firebase của mình. Nó sẽ có định dạng như sau: https: //.firebaseio.com Sao chép nội dung này.

(3) Nhận API REST Firebase cho Python. Chúng tôi sử dụng cái này: https://github.com/benletchford/python-firebase-gae Navigate to trên thư mục lib của bạn và chạy lệnh này sẽ đặt mã căn cứ hỏa lực vào thư mục lib của bạn:

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

(4) Trong tập "main.py" của bạn (hoặc bất kỳ bạn đang sử dụng) thêm mã này:

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) Thêm mã này vào MainHandler (giả sử bạn đang sử dụng 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 />') 

Bây giờ khi bạn điều hướng đến Cơ sở dữ liệu thời gian thực Firebase, bạn sẽ thấy các mục nhập cho Harry Potter với tư cách người dùng và những người khác.

Các vấn đề liên quan