【问题标题】:Python Requests giving response 401Python请求给出响应401
【发布时间】:2017-03-27 02:51:52
【问题描述】:

我正在从 API 下载数据但收到错误 401: 我了解错误 401,但不知道我的代码有什么问题。

import json
import requests
path='C:/Users/sharm_000/Desktop/Price Comparison/'

cat=str('https://affiliate-api.flipkart.net/affiliate/api/swapnilsh5.json')
r = requests.get(cat, auth=('swapnilsh5', '7018e1141bcd4ace9c3fe12277924035'))
print (r)

上面的代码返回 201 响应很好,但是当我进行下一级数据下载时,挑战就来了

link='https://affiliate-api.flipkart.net/affiliate/1.0/feeds/swapnilsh5/category/j9e-abm-c54.json?expiresAt=1479062969473&sig=1ef27c056140e0ff7cac143670584e9d&inStock=1'

r = requests.get(str(link), auth=('swapnilsh5', '7018e1141bcd4ace9c3fe12277924035'))

print(r)

这返回 401 错误,我无法弄清楚,当我使用 curl 运行上述链接时

curl -H "Fk-Affiliate-Id:swapnilsh5" -H "Fk-Affiliate-Token:7018e1141bcd4ace9c3fe12277924035" "https://affiliate-api.flipkart.net/affiliate/1.0/feeds/swapnilsh5/category/j9e-abm-c54.json?expiresAt=1479062969473&sig=1ef27c056140e0ff7cac143670584e9d&inStock=1" -o "C:\Users\sharm_000\Desktop\Price Comparison\a1c.json"

curl 命令运行良好。

请建议我哪里出错了,以及在 python 中执行相同任务的其他方法。

提前致谢。

【问题讨论】:

    标签: python api curl python-requests


    【解决方案1】:

    您要连接的 api 端点使用非标准标头。所以 auth 对你不起作用。您必须将它们作为自定义标头传递,就像您在使用 curl 时所做的那样。

    requests.get(str(link), 
        headers = { "Fk-Affiliate-Id" : 'swapnilsh5',
                    "Fk-Affiliate-Token": '7018e1141bcd4ace9c3fe12277924035'})
    

    【讨论】:

      猜你喜欢
      • 2017-12-27
      • 1970-01-01
      • 1970-01-01
      • 2020-04-28
      • 1970-01-01
      • 2018-01-22
      • 1970-01-01
      • 1970-01-01
      • 2020-07-14
      相关资源
      最近更新 更多