我正在尝试向我的python云函数添加授权。我在GCP项目中创建了一个服务帐户并生成了密钥。调用云函数的测试客户端代码(不在GCP中)如下所示:
从google.oauth2导入service_account从google.auth.transport.requests导入AuthorizedSessionSERVICE_ACCOUNT_FILE='<my_project_key_FILE>.json'credentials=服务帐户。凭据.from_service_account_file(service_account_file,scopes=['https://www.googleapis.com/auth/userinfo.email'])authed_session=授权会话(凭据)response=authd_session.get('http://<my_project>.cloudfunctions.net/authValidation')
我知道这段代码正确地从谷歌获得了JWT承载令牌,并被添加到我的云函数调用中的Authorization头中。我只是很难在云函数中验证该令牌。该代码的相关部分如下所示:
从google.oauth2导入id_token来自google.auth.transport导入请求def hello_world(请求):#来自https://developers.google.com/identity/sign-in/web/backend-auth#使用-a-google-api-客户端库idinfo=id_token.verify_oauth2_token(request.headers.get('Authorization')[7:]),个请求。请求())
我知道id标记是正确的,因为手动验证(使用https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=xxx)返回我期望的结果。
我得到的错误日志堆栈跟踪是:
回溯(最近一次调用):run_http_function中的文件“/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py”,第346行结果=_function_handler.invoke_user_function(flask.request)invoke_user_function中的文件“/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py”,第217行返回call_user_function(请求或事件)call_user_function中的文件“/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py”,第210行返回自我_用户功能(请求或事件)hello_world中的文件“/user_code/main.py”,第17行idinfo=id_token.verify_oauth2_token(request.headers.get('Authorization')[7:],请求。请求())文件“/env/local/lib/python3.7/site-packages/google/oauth2/id_token.py”,第141行,位于verify_oauth2_token中certs_url=_GOOGLE_OAUTH2_certs_url)文件“/env/local/lib/python3.7/site-packages/google/oauth2/id_token.py”,第122行,位于verify_token中return jwt.decode(id_token,certs=certs,audience=audience)文件“/env/local/lib/python3.7/site-packages/google/auth/jwt.py”,第219行,解码中标头,有效载荷,signed_section,signature=_unverified_decode(标记)文件“/env/local/lib/python3.7/site-packages/google/auth/jwt.py”,第139行,在_unverified_decode中header=_decode_jwt_segment(编码头)文件“/env/local/lib/python3.7/site-packages/google/auth/jwt.py”,第112行,位于_decode_jwt_segment中六、raise_from(new_exc,caught_exc)raise_from中的文件“<string>”,第3行ValueError:无法分析段:b'\xc9\xad\xbd'
我在这里错过了什么?谢谢