我想验证传递的令牌.问题是我在Google-API-python-clIEnt库不支持的服务器上运行python3.我在site中使用pyjwt和请求库找到了以下解决方法:
import Jsonimport jwtimport requestsGoogle_CERTS_URI = 'https://www.GoogleAPIs.com/oauth2/v1/certs'class GoogleIDToken(object): def __init__(self): self._certs = {} self._token = {} def getCerts(self): cert = requests.get(Google_CERTS_URI) if cert.status_code == 200: return Json.loads(cert.content) def isValID(self,token,audIEnce,clIEntID=None): self._certs = self.getCerts() for key in self._certs: try: token = jwt.decode(token,key=self._certs[key],verify=False) if 'email' in token and 'aud' in token: if token['aud'] == audIEnce and (clIEntID == token['cID'] if clIEntID is not None else True): self._token = token return True except Exception,e: print("Error deCoding: %s" % e.message) return False
我的两个问题是:
>有没有人知道在python3中工作的不同和/或更好的现有解决方案?
>上述解决方案是否完整?
依赖
pip install cryptography PyJWT requests
码
import jwt,requestsfrom cryptography.x509 import load_pem_x509_certificatefrom cryptography.hazmat.backends import default_backendGoogle_CERTS_URI = 'https://www.GoogleAPIs.com/oauth2/v1/certs'def verify_ID_token(ID_token,audIEnce): certs = requests.get(Google_CERTS_URI).Json() for cert in certs.values(): cert = str.encode(cert) cert_obj = load_pem_x509_certificate(cert,default_backend()) pub_key = cert_obj.public_key() try: return jwt.decode(ID_token,pub_key,algorithm='RS256',audIEnce=audIEnce) except (jwt.exceptions.DecodeError,jwt.exceptions.ExpiredSignatureError) as e: pass
编辑
我刚刚在python中实现了Google provides an example,使用了他们的oauth2client库.
@H_404_44@ 总结以上是内存溢出为你收集整理的验证Android后端调用Python中的Id令牌全部内容,希望文章能够帮你解决验证Android后端调用Python中的Id令牌所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)