我正在制作一个使用谷歌登录的应用程序。我还有一个链接到应用程序的 Django 后端,我想在 Django 后端验证用户。我在互联网上找到了许多解决方案,但没有一个有效。可能我在某个地方搞砸了。
我尝试使用 python-jose 进行验证,代码如下:
from jose import jwt
import urllib.request, json
token = '<token recieved using await user.getIdToken in flutter>'
target_audience = "<tried projectid/appid>"
certificate_url = 'https://www.googleapis.com/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com'
response = urllib.request.urlopen(certificate_url)
certs = response.read()
certs = json.loads(certs)
print(certs)
user = jwt.decode(token, certs, algorithms='RS256',
audience=target_audience)
我也试过oauth2client,代码在这里:
from oauth2client import crypt
import urllib.request, json
certificate_url = 'https://www.googleapis.com/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com'
target_audience = 'tried projectid/appid'
response = urllib.request.urlopen(certificate_url)
certs = response.read()
certs = json.loads(certs)
print(certs)
crypt.MAX_TOKEN_LIFETIME_SECS = 30 * 86400
idtoken = 'token received from await user.getIdToken()'
crypt.verify_signed_jwt_with_certs(idtoken, certs, target_audience)
我还为 python 尝试了 firebase_admin:
import firebase_admin
from firebase_admin import credentials
from firebase_admin import auth
cred = credentials.Certificate('<firebase service accounts private key>')
default_app = firebase_admin.initialize_app(cred)
token = 'token from flutter'
verifyied =auth.verify_id_token(id_token=token)
只是为了检查 firebase_admin 库本身是否正常工作,我将用户 ID 从应用程序传递到服务器,并尝试使用 firebase_admin 删除用户,我可以做到这一点。但由于某种原因,我无法验证令牌。
谢谢您的帮助。