Error 401
Error 401
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
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
Filters:
-
LP DAAC - dgolon
- User Services

- Posts: 194
- Joined: Tue Dec 03, 2024 2:37 pm America/New_York
- Endorsed: 2 times
Re: Error 401
Hi @mohaisin Which data are you trying to download?
Subscribe to the LP DAAC listserv by sending a blank email to lpdaac-join@lists.nasa.gov.
Sign up for the Landsat listserv to receive the most up to date information about Landsat data: https://public.govdelivery.com/accounts/USDOIGS/subscriber/new#tab1.
Sign up for the Landsat listserv to receive the most up to date information about Landsat data: https://public.govdelivery.com/accounts/USDOIGS/subscriber/new#tab1.
-
GES DISC - cbattisto
- Subject Matter Expert

- Posts: 29
- Joined: Wed Feb 16, 2022 4:38 pm America/New_York
Re: Error 401
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)
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)