407 Proxy Authentication Required on 2.27.1

About a week ago, on a previous requests version, this happened: when I tried to send a request (whichever) with a proxy (with the correct username and password), this error would appear: `ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 407 Proxy Authentication Required')`.

Then it got fixed in 2.27.1 and all seemed okay. But now, suddenly, this also happens on 2.27.1.
I can assure you the proxy dict has the correct data in it, so it's certainly a requests problem.
Can you please fix it?

Traceback:
```
Traceback (most recent call last):
  File "/home/user/.local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 696, in urlopen
    self._prepare_proxy(conn)
  File "/home/user/.local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 964, in _prepare_proxy
    conn.connect()
  File "/home/user/.local/lib/python3.9/site-packages/urllib3/connection.py", line 371, in connect
    self._tunnel()
  File "/usr/lib/python3.9/http/client.py", line 907, in _tunnel
    raise OSError(f"Tunnel connection failed: {code} {message.strip()}")
OSError: Tunnel connection failed: 407 Proxy Authentication Required

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/user/.local/lib/python3.9/site-packages/requests/adapters.py", line 440, in send
    resp = conn.urlopen(
  File "/home/user/.local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 755, in urlopen
    retries = retries.increment(
  File "/home/user/.local/lib/python3.9/site-packages/urllib3/util/retry.py", line 574, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='example.com', port=443): Max retries exceeded with url: /justanexample (Caused by ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 407 Proxy Authentication Required')))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user/.local/lib/python3.9/site-packages/requests/api.py", line 75, in get
    return request('get', url, params=params, **kwargs)
  File "/home/user/.local/lib/python3.9/site-packages/requests/api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "/home/user/.local/lib/python3.9/site-packages/requests/sessions.py", line 529, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/user/.local/lib/python3.9/site-packages/requests/sessions.py", line 645, in send
    r = adapter.send(request, **kwargs)
  File "/home/user/.local/lib/python3.9/site-packages/requests/adapters.py", line 513, in send
    raise ProxyError(e, request=request)
requests.exceptions.ProxyError: HTTPSConnectionPool(host='example.com', port=443): Max retries exceeded with url: /justanexample (Caused by ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 407 Proxy Authentication Required')))
```


Hi @cattieinthere, if it worked previously on 2.27.1 and now doesn't, it would imply something changed in the environment rather than Requests. Can you please provide the info we originally requested in the ticket template? There's nothing we can do with what's currently been provided.

just something as simple as:

```
import requests

r = requests.get(url, proxies=proxies)
```

I tried it now and it works but the exact same code on the same requests version didn't when I wrote this post!

When you open a ticket, it asks for the output of `python -m requests.help`. Can you please provide the contents of that command?

Yes, sorry.

```
{
  "chardet": {
    "version": "4.0.0"
  },
  "charset_normalizer": {
    "version": "2.0.8"
  },
  "cryptography": {
    "version": "36.0.0"
  },
  "idna": {
    "version": "3.3"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.9.5"
  },
  "platform": {
    "release": "5.11.0-41-generic",
    "system": "Linux"
  },
  "pyOpenSSL": {
    "openssl_version": "101010cf",
    "version": "21.0.0"
  },
  "requests": {
    "version": "2.27.1"
  },
  "system_ssl": {
    "version": "1010106f"
  },
  "urllib3": {
    "version": "1.26.7"
  },
  "using_charset_normalizer": false,
  "using_pyopenssl": true
}

```

So from the debug log, I don't see anything out of the ordinary. Is this the same machine/python runtime that was previously working? Are the proxy credentials sourced directly from your input to proxies?

The next steps for things to check would be:
1. Confirm the Requests version at runtime
```python
import requests

print(requests.__version__)

# Verify this is identical to what you passed in
print(requests.utils.prepend_scheme_if_needed(proxy_url, 'http'))

r = requests.get(url, proxies=proxies)
```
2. Confirm the proxies dictionary and URI format. Does it match the below?
```python
proxies = {
    'http': 'http://<username>:<password>@<host>:<port>'
    'https': 'https://<username>:<password>@<host>:<port>'
}
```
3. Does downgrading back to 2.25.1 fix the authentication issue?

Yes, the same machine! The proxy credentials are directly sourced from my input, yes.

- As for `1,`, yes, everything is okay.

```
>>> print(requests.__version__)
2.27.1
>>> print(requests.utils.prepend_scheme_if_needed(proxy_url, 'http'))
'http://<username>:<password>@rotating-residential.myproxy.host:<port>'
```

- As for `2.`, I use `http://` for the `https` key because if I try with `https`

```
proxies = {
    'http': 'http://<username>:<password>@rotating-residential.myproxy.host:<port>'
    'https': 'https://<username>:<password>@rotating-residential.myproxy.host:<port>'
}
```

This happens:

`SSLError(SSLCertVerificationError("hostname 'rotating-residential.myproxy.host' doesn't match '*.rotating.myproxy.host'")`

- As for `3.`, no, `2.25.1` doesn't fix it. It's the same.

BTW, when I created this post it didn't work, then it worked and now it's again not working. In the same machine and with the same code.


If it's intermittent, I'm not sure we can tie the failure to Requests given that you've just validated the pieces involved which are all static.  We've also proven it's unrelated to any of the proxy issues in the last two versions, so I don't believe this is a new bug. If the parsed URI from `prepend_scheme_if_needed` isn't missing the `userinfo` portion, that should all get passed along.

At this point we'd need more info from the proxy on _why_ it's rejecting the request because I'm not seeing anything suggesting this is actually a problem with the library.

Hi @cattieinthere, checking in to see if you're still experiencing this issue or have any more info. I think we'll likely resolve this next week if we don't have an update as there aren't further actions we can take.

Going to resolve since we haven't heard anything further. Feel free to reopen if you find any new information. Thanks!

> Going to resolve since we haven't heard anything further. Feel free to reopen if you find any new information. Thanks!

I'd like to just make known that I had the exact problem on 2.27.0, downgrading to 2.26.0 fixed it for me

I second this. Downgrading to 2.26.0 fixed it for me.