swapnil sharma swapnil sharma - 19 days ago 4
Python Question

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.

Answer

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'})
Comments