首页 > 解决方案 > 如何从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。

标签: pythonangularflask

解决方案


你需要修改你的代码,如下所示

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

推荐阅读