python - 验证 firebase 身份验证 token 权限被拒绝

标签 python google-app-engine firebase firebase-authentication google-oauth

按照 google docs 中的说明进行操作为了在 GAE 中使用 firebase 进行身份验证,我将一个授权 token 从 Android 发送到我的后端 python 服务器。使用以下代码读取该 token :

import google.auth.transport.requests
import google.oauth2.id_token

HTTP_REQUEST = google.auth.transport.requests.Request()
id_token = headers['authorization'].split(' ').pop()
user_info = google.oauth2.id_token.verify_firebase_token(
    id_token, HTTP_REQUEST)

产生以下堆栈跟踪:

  File "/Users/alex/projects/don/don_server/mobile/main.py", line 61, in get_video
    user_id = get_user_id(self.request_state.headers)
  File "/Users/alex/projects/don/don_server/mobile/main.py", line 37, in get_user_id
    id_token, HTTP_REQUEST)
  File "/Users/alex/projects/don/don_server/mobile/lib/google/oauth2/id_token.py", line 115, in verify_firebase_token
    id_token, request, audience=audience, certs_url=_GOOGLE_APIS_CERTS_URL)
  File "/Users/alex/projects/don/don_server/mobile/lib/google/oauth2/id_token.py", line 76, in verify_token
    certs = _fetch_certs(request, certs_url)
  File "/Users/alex/projects/don/don_server/mobile/lib/google/oauth2/id_token.py", line 50, in _fetch_certs
    response = request(certs_url, method='GET')
  File "/Users/alex/projects/don/don_server/mobile/lib/google/auth/transport/requests.py", line 111, in __call__
    raise exceptions.TransportError(exc)
TransportError: ('Connection aborted.', error(13, 'Permission denied'))

我已经仔细检查了我的 Firebase 项目设置,并且 localhost 在身份验证登录部分中被列为授权域(我在 GAE 本地开发服务器上运行它)。

据我所知,这在几周前就有效了。有什么想法吗?

更新:

我使用 firebase docs 中建议的服务帐户实现了相同的身份验证。但我收到相同的错误消息:

from firebase_admin import auth, credentials
import firebase_admin

fpath = os.path.join(os.path.dirname(__file__), 'shared', 'firebase-admin-private-key.json')
cred = credentials.Certificate(fpath)
firebase_admin.initialize_app(cred)

然后处理传入的 token

id_token = headers['authorization'].split(' ').pop()
user_info = auth.verify_id_token(id_token)

最佳答案

在某个时候,我升级了我的 requests 库。因为请求 doesn't play well使用 GAE,对 firebase 服务器的调用失败。通过降级到版本 2.3.0,现在可以使用了。

pip install -t lib requests==2.3.0

或者monkeypatching requests正如 this answer 中所建议的也有效!

import requests_toolbelt.adapters.appengine

requests_toolbelt.adapters.appengine.monkeypatch()

关于python - 验证 firebase 身份验证 token 权限被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45041767/

相关文章:

python - 无法设置属性和无限递归

java - 未创建 GAE 数据存储索引

android - 在Android中使用Firebase时发生Appcompat错误

python - 克服 python 中的 appengine 500 字节字符串限制?考虑文本

node.js - Firebase 身份验证与云 sql 混合

android - 找不到 com.google.firebase :firebase-crashlytics flutter

python - 将每日 OHLCV 重新采样为每周 OHLCV

python - 如何在 py.test 中使用多核?

python - 如何创建没有主键的Storm表?

python - 如何从处理程序中的 blobstore 检索文件对象?