diff --git a/R/blackmarbler.R b/R/blackmarbler.R index 11ac2a2..34139a4 100644 --- a/R/blackmarbler.R +++ b/R/blackmarbler.R @@ -528,48 +528,23 @@ download_raster <- function(file_name, if(quiet == FALSE) message(paste0("Processing: ", file_name)) - - if(quiet == TRUE){ - - url1 <<- url - # response <- httr::GET(url, - # httr::timeout(60), - # httr::add_headers(headers), - # httr::write_disk(download_path, overwrite = TRUE)) - - response <- httr2::request(url) %>% - httr2::req_headers('Authorization' = paste('Bearer', bearer)) %>% - httr2::req_timeout(60) %>% - httr2::req_perform() - - writeBin(httr2::resp_body_raw(response), download_path) - - - } else{ - - response <- httr2::request(url) %>% - httr2::req_headers('Authorization' = paste('Bearer', bearer)) %>% - httr2::req_timeout(60) %>% - httr2::req_perform() - - writeBin(httr2::resp_body_raw(response), download_path) - - # response <- httr::GET(url, - # httr::timeout(60), - # httr::add_headers(headers), - # httr::write_disk(download_path, overwrite = TRUE), - # httr::progress()) - - } + response <- httr2::request(url) %>% + httr2::req_headers('Authorization' = paste('Bearer', bearer)) %>% + httr2::req_timeout(60) %>% + httr2::req_perform() if(response$status_code != 200){ - message("Error in downloading data") message(response) + stop("Error in downloading data") + } + + if(response$status_code == 200){ + if(length(response$body) < 10000){ + stop(paste0("Issue with bearer token. You may need to generate a new token. Ensure that select EULAs are accepted. Please see the instructions here: https://github.com/worldbank/blackmarbler?tab=readme-ov-file#bearer-token-")) + } } - #if(response$all_headers[[1]]$status != 200){ - # message("**Error in downloading data; bearer token likely invalid.** Try regenerating the bearer token; please see this link for instructions to obtain a bearer token: https://github.com/worldbank/blackmarbler?tab=readme-ov-file#bearer-token-") - #} + writeBin(httr2::resp_body_raw(response), download_path) } diff --git a/README.md b/README.md index ae6da0b..05cb8a1 100644 --- a/README.md +++ b/README.md @@ -49,15 +49,17 @@ Follow the below steps to obtain a bearer token: 1. Create a [NASA Earth Data account](https://ladsweb.modaps.eosdis.nasa.gov/) account. On the top right, click "Login" then "Earthdata Login". Then click "register" (blue button). 2. Enter the information in the registration page. You __must__ include the following information; this information is not required to create an account, but the bearer token will not work without this information: - - Study Area - - User Type - - Organization + + - Study Area + - User Type + - Organization 3. Click "Register for EarthData Login" (green button at bottom). Check your email, and click the link in the email to activate the account. 4. Go to the [Earth Data Login](https://urs.earthdata.nasa.gov/users) page and login. 5. On the panel near the top, click "EULAs" then "Accept New EULAs". Accept: - - MERIS EULA - - Sentinel EULA + + - MERIS EULA + - Sentinel EULA 6. On the "Profile Home" page, you should see something like below. Information should be filled in for each category, and "Agreed To Meris EULA" and "Agreed To Sentinel-3 EULA" should be True. @@ -77,7 +79,7 @@ Follow the below steps to obtain a bearer token: ### Programmatically retrieve token -After following the above steps, the bearer token can be programmatically retrieved using the `get_nasa_token()` function and your usename and password. +After following the above steps, the bearer token can also be programmatically retrieved using the `get_nasa_token()` function and your usename and password. ```r bearer <- get_nasa_token(username = "USERNAME-HERE",