Page 1 of 1

Error 401

Posted: Mon Dec 15, 2025 6:44 am America/New_York
by mohaisin
I am trying to download data using RStudio:
response <- GET(
url,
authenticate(username, password),
write_disk(dest_path, overwrite = TRUE),
progress()
)
I downloaded using the same code a few months ago, but now I keep receiving Error 401, which is related to authentication.

Please advise,
Thx

Re: Error 401

Posted: Mon Dec 15, 2025 8:45 am America/New_York
by LP DAAC - dgolon
Hi @mohaisin Which data are you trying to download?

Re: Error 401

Posted: Mon Dec 15, 2025 10:24 am America/New_York
by mohaisin

Re: Error 401

Posted: Wed Dec 17, 2025 2:48 pm America/New_York
by GES DISC - cbattisto
Hello,

The authenticate() module in the httr library uses basic auth, which is no longer supported with Earthdata Login. Instead, a .netrc file will need to be created to store credentials, and then the config() parameter will need to be passed to ensure that the .netrc is being used to authenticate.

Here is R code that will use the earthdatalogin package to create the .netrc, before being used with the httr library to download the CSV from Cloud OPeNDAP:

library(earthdatalogin)
library(httr)

# Only need to run once to create and store .netrc
earthdatalogin::edl_netrc(
username = "YOUR_EDL_USERNAME",
password = "YOUR_EDL_PASSWORD"
)

url <- "https://opendap.earthdata.nasa.gov/collections/C1276812830-GES_DISC/granules/M2T1NXAER.5.12.4%3AMERRA2_400.tavg1_2d_aer_Nx.20170101.nc4.dap.csv?dap4.ce=/BCSMASS%5B0:23%5D%5B212:249%5D%5B339:377%5D;/DUSMASS%5B0:23%5D%5B212:249%5D%5B339:377%5D;/DUSMASS25%5B0:23%5D%5B212:249%5D%5B339:377%5D;/OCSMASS%5B0:23%5D%5B212:249%5D%5B339:377%5D;/SO4SMASS%5B0:23%5D%5B212:249%5D%5B339:377%5D;/SSSMASS%5B0:23%5D%5B212:249%5D%5B339:377%5D;/SSSMASS25%5B0:23%5D%5B212:249%5D%5B339:377%5D;/time;/lat%5B212:249%5D;/lon%5B339:377%5D"

dest_path <- "merra2_subset.csv"

response <- GET(
url,
config(netrc = TRUE), # Ensure that .netrc is being used to authenticate
write_disk(dest_path, overwrite = TRUE),
progress()
)

stop_for_status(response)