I'm trying to access a protected image on a website. If I use my broswer and log in, I can easily see the image. Now I'm trying to do the same using python.
I came across this question which seemed like the same issue that I have. With that I wrote the following program:
import requests
session = requests.Session()
session.get("https://www.page.com/login")
response = session.post("https://www.page.com/login", data = {"cmd": "login", "username": "myusername", "password": "mypass"})
print response.text
Output here is:
{"status":"ok","message":"Successful login.","referer":"https:\/\/page.com\/"}
I've also saved the response.text into a .html file, which confirmed that I've logged in. If at this point I open any subpage on page.com, I'm still logged in.
Then I try to access the protected image at url:
with open('image.html', 'wb') as file:
response = session.get(url, headers = {"User-Agent": "Mozilla/5.0"})
print response.status_code
file.write(response.text)
file.close()
Which gives the output of 403. If I open the image.html file there's an "Item not available".
I've tried doing all of this using mechanize and cookielib instead of requests.Session() where I created a cookiejar, created a Browser() object, successfully logged in but as I tried accessing that image, I had the same 403 error. Therefore any ideas will be greatly appriciated.