简体   繁体   English

Python请求给出响应401

[英]Python Requests giving response 401

I am downloading the data from API but getting error 401: I understand the error 401 but dont know what is wrong in my code. 我从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)

the above code returns 201 response which is great, but the challenge comes when i go for next level of data download 上面的代码返回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)

this return 401 error, this i am not able to figure out, when I run the above link using curl 这个返回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"

The curl commands work absolutely fine. curl命令工作得非常好。

Please suggest where I am going wrong and what would be the other ways of doing the same task in python. 请告诉我出错的地方以及在python中执行相同任务的其他方法。

Thanks in Advance. 提前致谢。

The api endpoint that you are connecting to uses non standard headers. 您要连接的api端点使用非标准标头。 So auth will not work for you. 因此,auth不适合您。 you have to pass them as custom headers, exactly as you have done when using curl. 你必须将它们作为自定义标题传递,就像使用curl时一样。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM