2016-04-03 19 views
5

私はpython(2.7)からfirebaseのデータを取得しようとしています。PythonのFirebaseユーザ認証

enter image description here

そして最後に、私のpythonコード:

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 
ここ
{ 
    "rules": { 
     "user": { 
      "$uid": { 
      ".read": "auth != null && auth.uid == $uid", 
      ".write": "auth != null && auth.uid == $uid" 
      } 
     } 
    } 
} 

は私databseのスクリーンショットです:ここで

は(firebseio.com上)私のルールです

今、私はデータを取得し、firebaseとの間でデータをやりとりする方法を理解できません。 は、私はラインuseingしようと試み: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',)) 

誰でも作業コードを私に提供することはできますか?事前に

おかげで、

+0

DSNを使用していない場合はどうなりますか? – marciokoko

答えて

6

のZviカープは、ここでは、認証作業を取得するために要した手順です。

(1)まず、Firebase Secretが必要です。 Firebaseでプロジェクトを作成したら、[設定]をクリックします。 [データベース]をクリックし、秘密情報を作成することを選択します。 settings

あなたの秘密をコピーしてください。それは後であなたのコードに入ります。

secret

(2)あなたは、あなたのfirebase URLを必要としています。 次のような形式になります。https://.firebaseio.com これもコピーします。

(3)Python用のFirebase REST APIを入手してください。あなたのlibディレクトリ上にhttps://github.com/benletchford/python-firebase-gae 移動し、あなたのlibディレクトリにfirebaseコードを配置します。このコマンドを実行します: 私たちは、このいずれかを使用し

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

Firebase Realtime Databaseに移動すると、Harry Potterのユーザーとその他のエントリが表示されます。

関連する問題