简体   繁体   中英

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.

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

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

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.

Please suggest where I am going wrong and what would be the other ways of doing the same task in python.

Thanks in Advance.

The api endpoint that you are connecting to uses non standard headers. So auth will not work for you. you have to pass them as custom headers, exactly as you have done when using curl.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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