requests icon indicating copy to clipboard operation
requests copied to clipboard

authentication via netrc has a different outcome than auth argument

Open itcarroll opened this issue 3 years ago • 0 comments
trafficstars

NASA Earthdata uses basic authentication and cookies to respond to GET requests. When I use a Session() in combination with a ~/.netrc file, the session object holds the cookies and everything works. When I use a Session() but supply my credentials with the auth argument, the session cannot hold its cookies. Authentication failure results. It would be great if the auth argument could do whatever the session does for netrc authentication.

To reproduce, you will have to have to follow these instructions for a free account: https://disc.gsfc.nasa.gov/earthdata-login

Expected Result

<Response [200]>

Actual Result

<Response [401]>

Reproduction Steps

Have a NASA Earthdata account with <USERNAME> and <PASSWORD> stored in a text file at ~/.netrc with permissions -rw------- and containing the line:

machine urs.earthdata.nasa.gov login <USERNAME> password <PASSWORD>

The following returns the expected result

from requests import Session

resource = 'https://reason.gesdisc.eosdis.nasa.gov/data/nobm/NOBM_DAY.R2017/2015/day20151201.R2017.nc4'
with Session() as session:
    response = session.get(resource)
response

The following returns the actual result

from requests import Session

resource = 'https://reason.gesdisc.eosdis.nasa.gov/data/nobm/NOBM_DAY.R2017/2015/day20151201.R2017.nc4'
with Session() as session:
    response = session.get(resource, auth=('<USERNAME>', '<PASSWORD>'))
response

System Information

$ python -m requests.help
{
  "chardet": {
    "version": null
  },
  "charset_normalizer": {
    "version": "2.1.1"
  },
  "cryptography": {
    "version": "38.0.1"
  },
  "idna": {
    "version": "3.4"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.10.6"
  },
  "platform": {
    "release": "21.6.0",
    "system": "Darwin"
  },
  "pyOpenSSL": {
    "openssl_version": "30000050",
    "version": "22.0.0"
  },
  "requests": {
    "version": "2.28.1"
  },
  "system_ssl": {
    "version": "1010111f"
  },
  "urllib3": {
    "version": "1.26.12"
  },
  "using_charset_normalizer": true,
  "using_pyopenssl": true
}

itcarroll avatar Sep 22 '22 14:09 itcarroll