按照google docs中的指示,在GAE中使用firebase for auth,我将从Android发送授权令牌到我的后端python服务器。使用以下代码读取该令牌:
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)
然后处理传入令牌
id_token = headers['authorization'].split(' ').pop()
user_info = auth.verify_id_token(id_token)
答案 0 :(得分:1)
在某些时候,我升级了我的requests
库。由于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()