【问题标题】:How to get the value of a header from URL in python? [duplicate]如何从python中的URL获取标头的值? [复制]
【发布时间】:2021-01-18 22:40:47
【问题描述】:

我有一个角度 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。

【问题讨论】:

    标签: python angular flask


    【解决方案1】:

    你需要像下面这样修改你的代码

    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
    

    【讨论】:

    • 我已经尝试过这种方法,但我的令牌变量始终为空。我还需要检查什么吗?
    • @SoubhagyaSahoo 试试print(request.headers) 看看你是否得到了标题
    猜你喜欢
    • 2020-09-05
    • 2012-03-31
    • 2019-03-28
    • 1970-01-01
    • 2011-04-08
    • 2018-07-21
    • 1970-01-01
    • 2018-10-28
    • 2021-09-21
    相关资源
    最近更新 更多