2016-11-13 71 views
1

我正在从API下载数据,但得到错误401: 我了解错误401,但不知道我的代码中出了什么问题。Python请求给予回应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 -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中执行相同任务的其他方法是什么。

在此先感谢。

回答

0

要连接到的api端点使用非标准标题。所以auth不会为你工作。您必须将它们作为自定义标题传递,与使用curl时完全相同。

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