简体   繁体   English

如何在Google AppEngine上验证Python脚本以使用Google Firebase?

[英]How to authenticate a Python script on Google AppEngine to use Google Firebase?

Google为Node.js,Android和iOS身份验证提供了很好的示例,以便连接到Firebase以使用Firebase实时数据库 - 但是如何通过Python从Google AppEngine应用程序连接到Firebase实时数据库并进行正确身份验证?

Here are the steps we took to get this working. 以下是我们为实现这一目标而采取的步骤。

(1) First you need a Firebase Secret. (1)首先你需要一个Firebase秘密。 After you have a project in Firebase, then click Settings. 在Firebase中安装项目后,单击“设置”。 Then click Database and choose to create a secret. 然后单击“数据库”并选择创建密码。 设置

Copy your secret. 复制你的秘密。 It will go into your code later. 它将在稍后进入您的代码。

秘密

(2) You need your firebase URL. (2)您需要您的firebase URL。 It will have the format like this: https://.firebaseio.com Copy this too. 它的格式如下:https://.firebaseio.com也可以复制它。

(3) Get a Firebase REST API for Python. (3)获取Python的Firebase REST API。 We used this one: https://github.com/benletchford/python-firebase-gae Navigate to above your lib directory and run this command which will place the firebase code into your lib directory: 我们使用了这个: https//github.com/benletchford/python-firebase-gae导航到lib目录上方并运行此命令,将firebase代码放入lib目录:

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

(4) In your "main.py" file (or whatever you are using) add this code: (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) Add this code to the MainHandler (assuming you are using AppEngine): (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 />')

Now when you navigate to your Firebase Realtime Database, you should see entries for Harry Potter as a user and others. 现在,当您导航到Firebase实时数据库时,您应该看到Harry Potter作为用户和其他人的条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM