python - 如何从python中的URL获取标头的值?
问题描述
我有一个角度 UI,当用户使用 google 登录并通过标头发送生成的访问令牌时,它会进行 API 调用。API 调用部分如下所示:
this.http.get(
`${environment.nodeURL}/api/myApiCall`,
{ headers: { authorization: this.accessToken}
})
该 API 是使用 flask-Python3 制作的。我想将该访问令牌存储在 API 中的局部变量上。谁能建议我如何获得价值?我尝试通过以下方式获取值:
@api.route('/verify_token')
@api.response(404, 'Invalid access_token')
class UserCheck(Resource):
def get(self):
""" Checks if the access token is valid or not """
response = urllib.request.urlopen('http://localhost:5000/user/verify_token')
print("Header value is...............",response.getheader('access_token'))
我目前正在 localhost 中运行 UI 和 API。
解决方案
你需要修改你的代码,如下所示
from flask import request
@api.route('/api/myApiCall')
class UserCheck(Resource):
def get(self):
""" Checks if the access token is valid or not """
token = request.headers.get('authorization')
# write your token validation code here
推荐阅读
- python - Yolo v3 模型输出澄清与 keras
- algorithm - 跨组选择联合的下限
- tcp - GCP 中有没有办法对在不同区域的 GKE 中运行的 sftp 服务进行负载均衡?
- rpa - 在 Automation Anywhere 中动态初始化数组值的最佳方法是什么?
- postgresql - PostgreSQL 上的 Liquibase dropDefaultValue 映射到“SET DEFAULT”而不是“DROP DEFAULT”
- angular - ngx-translate 即时函数不是抛出的函数:单元测试
- html - 弹性盒和媒体查询有什么区别?
- ruby - 我可以使用什么方法来通知自己由于任何原因未按计划运行的作业?(OOM等)
- sql-server - 从字符串转换日期和/或时间时错误转换失败
- mysql - MySql 复制 - 主作为从,从作为主