python 3.7 urllib.request.urlretrieve HTTP Error 403: Forbidden

Salute to all! You need to download the image from the site, but in order to download it, you need to log in. Authorization is successful, but the HTTP Error 403: Forbidden{[4] response is still received during the download]}

http = urllib3.PoolManager(ca_certs=certifi.where())

payload = {'UserName': 'Login', 'UserPassword': 'Password'}
encoded_data = json.dumps(payload).encode('utf-8')

resp = http.request(
     'POST',
     'https://mysite.ru/Login',
     body=encoded_data,
     headers={'Content-Type': 'application/json'})

data = json.loads(resp.data.decode('utf-8'))
urllib.request.urlretrieve("https://mysite.ru/img/1", f"tmp/test.jpg")

I also tried to use requests, but the server did not accept authorization from it.


Used again requests

s = requests.session()
headers = {
    
    'Content-Type': 'application/json',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36',
    'ForceUseSession': 'true'
}
data={'UserName': 'UserName': 'Login', 'UserPassword': 'Password'}
p = s.post('https://mysite.ru/Login', headers=headers, data=data)
s.cookies

Got a response:

        requests.packages.urllib3.connectionpool - DEBUG - "POST /Login HTTP/1.1" 400 3069
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Request Error</title>
  </head>
  <body>
    <div id="content">
      <p class="heading1">Request Error</p>
      <p>The server encountered an error processing the request. The exception message is 'There was an error checking start element of object of type Core.ServiceModelContract.AuthToken. Encountered unexpected character 'U'.'. See server logs for more details. The exception stack trace is: </p>
      <p>   at System.Runtime.Serialization.XmlObjectSerializer.IsStartObjectHandleExceptions(XmlReaderDelegator reader)
   at System.ServiceModel.Dispatcher.SingleBodyParameterMessageFormatter.ReadObject(Message message)
   at System.ServiceModel.Dispatcher.SingleBodyParameterDataContractMessageFormatter.ReadObject(Message message)
   at System.ServiceModel.Dispatcher.SingleBodyParameterMessageFormatter.DeserializeRequest(Message message, Object[] parameters)
   at System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message message, Object[] parameters)
   at System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters)
   at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc&amp; rpc)
   at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp; rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc&amp; rpc)
   at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</p>
    </div>
  </body>
</html>
Author: Amicrone, 2020-12-02