通过带有访问令牌的 python 脚本获取请求

GET request via python script with access token

我在通过 python 使用访问令牌执行 GET 请求时遇到了一些困难。 出于某种原因,当我执行来自 POSTMAN 的请求时,我得到了预期的结果,但是,当使用 python 时,我收到连接错误:

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

我相信我的脚本被服务器阻止以防止报废尝试,但我不确定。 这就是我 运行 通过 POSTMAN :

https://www.operation.io/web/alerts?access_token=6ef06fee265de1fa640b6a444ba47--z&envId=58739be2c25e76a202c9dd3e&folderId=active&sortBy=status

这就是我 运行 来自 脚本 的内容:

response = requests.get('https://www.operation.io/web/alerts', headers={'Authorization': 'access_token 6ef06fee265de1fa640b6a444ba47--z'})

请注意url和访问令牌不是真实的,所以请不要尝试使用。 我怎样才能通过脚本而不是通过邮递员让它工作? 谢谢你的帮助/

由于您是通过邮递员将令牌作为查询参数发送的(而不是 header),您可以试试这个:

response = requests.get('https://www.operation.io/web/alerts', params={'access_token': '6ef06fee265de1fa640b6a444ba47--z'})

假设您使用的 API 接受它作为查询参数而不是 headers(这是我从邮递员请求中猜测的)

如果不能实际测试解决方案就很难说,但我会建议 2 个过去对我有用的技巧:

  1. 将 "Authorization" 更改为 "authorization"
  2. 将“6ef06fee265de1fa640b6a444ba47--z”更改为 "Token 6ef06fee265de1fa640b6a444ba47--z"(同时添加一个 space)

全部放在一起:

response = requests.get('https://www.operation.io/web/alerts', headers={'authorization': 'Token 6ef06fee265de1fa640b6a444ba47--z'})