【问题标题】:how to send access token directly through browser如何通过浏览器直接发送访问令牌
【发布时间】:2018-09-26 01:38:43
【问题描述】:

这是我接受 HTTP GET 请求的函数:

class Test2(Resource):
        @jwt_required
        def get(self):
            mailid = request.args.get('mailid', '')
            username = request.args.get('username', '')
            password = request.args.get('password', '')
            print("Mail ID, Username, Password = ")
            print(mailid, username, password)
            return 'Not supported'

我在尝试以下 URL 时收到错误“缺少授权标头”:

http://127.0.0.1:5000/test2?mailid=ABC.xyz.com&username=ABC&password=password&jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0eXBlIjoiYWNjZXNzIiwiZXhwIjoxNTIzODc0MzAyLCJpYXQiOjE1MjM4NzM0MDIsImZyZXNoIjpmYWxzZSwibmJmIjoxNTIzODczNDAyLCJqdGkiOiI2NzYxNzdmNS05NGE3LTQzYTUtYjVhYy1mMmVlMzhhN2JhOWEiLCJpZGVudGl0eSI6MTIzfQ.iXZ9_qDtTw5_Q2VxEpvv5pYVAaI8M7P_CVy2Ln79eok

【问题讨论】:

  • 该错误告诉您 API 需要一个 Authorization 标头,可能类似于“Authorizaion: Bearer eyJ0e ....”。但是您将令牌作为请求参数发送。最好使用 Fiddler 或 Postman 来测试你的 API
  • 你能分享一下 jwt_required 包装器的代码吗?
  • 我清理了文本并将 URL 重新格式化为代码。
  • 嘿@nitish albin,我看到你接受了我的回答,然后又撤销了接受 - 你有什么特别要找的东西,或者我可以帮你吗?

标签: python browser get jwt flask-jwt


【解决方案1】:

如果您使用的是 requests,那么您可以像这样访问标头:

r = requests.get('https://api.github.com/user', auth=('user', 'pass'))
# this is just an example from http://docs.python-requests.org/en/master/
headers = r.headers

这里的 headers 是一个字典,并且具有 content-type 之类的键。查看文档以获取更多信息。

从您的代码看来,装饰器 @jwt_required 检查请求标头中的 auth 字段的标头 - 您应该查看那里。 如果您可以编辑问题并添加代码,您可以获得更具体的帮助。

【讨论】:

    猜你喜欢
    • 2012-06-01
    • 2020-11-05
    • 2016-08-21
    • 1970-01-01
    • 2019-01-05
    • 2016-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多