简体   繁体   English

HTTP错误400:使用Python对Firebase的错误请求

[英]HTTP Error 400: Bad request to Firebase using Python

I am using the python-firebase library by Ozgur Vatansever for my program. 我正在使用Ozgur Vatansever开发的python-firebase库

I have previously been able to write/read data to my firebase, however with the introduction of authentication I cannot. 我以前能够向Firebase写入/读取数据,但是随着身份验证的引入,我无法这样做。 Using: 使用:

from firebase import firebase

auth = firebase.FirebaseAuthentication('MY_FIREBASE_SECRET','myEmail@email.com') 
myFirebase = firebase.FirebaseApplication('https://MYFIREBASE.firebaseio.com', auth)

result = myFirebase.get('/users', None) 
print result

Resulting in: 导致:

HTTPError: 400 Client Error: Bad Request

Conversely, using no authentication: 相反,不使用身份验证:

 myFirebase = firebase.FirebaseApplication('https://MYFIREBASE.firebaseio.com', None)        
 result = myFirebase.get('/users', None)

works as expected. 如预期般运作。 Any help in why I am getting this HTTP error: 400 , when using authentication is greatly appreciated. 在使用身份验证时为什么收到此HTTP error: 400任何帮助,将不胜感激。

I'm using version 1.2 of the library and python 2.7 我正在使用库的1.2版本和python 2.7
In here there's an example on how to connect easily using auth. 这里有一个示例,说明如何使用auth轻松连接。

import datetime

from firebase.firebase import FirebaseApplication, FirebaseAuthentication


if __name__ == '__main__':
    SECRET = '12345678901234567890'
    DSN = 'https://firebase.localhost'
    EMAIL = 'your@email.com'
    authentication = FirebaseAuthentication(SECRET,EMAIL, True, True)
    firebase = FirebaseApplication(DSN, authentication)

    firebase.get('/users', None,
        params={'print': 'pretty'},
        headers={'X_FANCY_HEADER': 'very fancy'})

data = {'name': 'Ozgur Vatansever', 'age': 26,
    'created_at': datetime.datetime.now()}

snapshot = firebase.post('/users', data)
print(snapshot['name'])

def callback_get(response):
    with open('/dev/null', 'w') as f:
        f.write(response)
firebase.get_async('/users', snapshot['name'], callback=callback_get)

这是一个测试应用程序,所以秘密没有问题 这是上传数据的证明 Hope this helps! 希望这可以帮助!

I found the same problem, and the problem seems to be that Firebase is rejecting the token that python-firebase builds (in FirebaseTokenGenerator). 我发现了同样的问题,问题似乎出在Firebase拒绝了python-firebase构建的令牌(在FirebaseTokenGenerator中)。

I don't know what is wrong with the token being generated, and given that I am happy to use the JWT token provided in the Secrets section of the Firebase Dashboard, this hack works for me: 我不知道所生成的令牌有什么问题,并且鉴于我很乐意使用Firebase信息中心“机密信息”部分中提供的JWT令牌,因此该黑客适用于我:

In firebase.py, class FirebaseAuthentication, right at the start of the get_user method, add an if clause: 在firebase.py的FirebaseAuthentication类中,就在get_user方法的开头,添加一个if子句:

def get_user(self):
    if self.extra.get('explicitAuth') is None:
            token = self.authenticator.create_token(self.extra)
    else:
            token = self.extra.get('explicitAuth')

Then when invoking the application provide the explicit token overriding the generated one: 然后,在调用应用程序时,提供显式令牌覆盖所生成的令牌:

auth = firebase.FirebaseAuthentication('Ignore', 'Ignore', extra={'explicitAuth': myToken})
app = firebase.FirebaseApplication('https://mystuff.firebaseio.com/', auth)

Maybe you should have a look at the docs Custom Authentication 也许您应该看看docs 自定义身份验证

The library you are using takes python-firebase to generate tokens. 您正在使用的库使用python-firebase生成令牌。 In your code, 在您的代码中

auth = firebase.FirebaseAuthentication('MY_FIREBASE_SECRET','myEmail@email.com')

it should be: 它应该是:

auth = firebase.FirebaseAuthentication('MY_FIREBASE_SECRET','myEmail@email.com', extra={'uid': '123'})

The param extra is the payload to create token and uid is a must as the github page says. extra的参数是创建令牌的有效载荷,而uid是必须的,如github页面所说。

look at the file firebase.py , class FirebaseApplication function _authenticate : 看文件firebase.py ,类FirebaseApplication功能_authenticate

replace 'auth' by 'auth_token' in params.update({'auth_token': user.firebase_auth_token}) params.update({'auth_token': user.firebase_auth_token}) 'auth'替换为'auth_token'

fixes this HTTPError: 400 Client Error: Bad Request error but don't fix all auth problems... 修复了此HTTPError: 400 Client Error: Bad Request错误,但没有解决所有身份验证问题...

Let's try this, Go to Rules tab and set true 让我们尝试一下,转到“规则”标签并设置为true

{
  "rules": {
    ".read": "true",
    ".write": "true"
  }
}

Code from firebase import firebase 从Firebase导入Firebase的代码

myFirebase = firebase.FirebaseApplication('https://MYFIREBASE.firebaseio.com', None)

result = myFirebase.get('/users', None) 
print result

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

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