Skip to content

Instantly share code, notes, and snippets.

@cboettig
Last active January 23, 2024 18:55
Show Gist options
  • Save cboettig/5401bd149a2a27bde2042aa4f7cde25b to your computer and use it in GitHub Desktop.
Save cboettig/5401bd149a2a27bde2042aa4f7cde25b to your computer and use it in GitHub Desktop.
Use earthdata tokens with GDAL to access data via https links
## examples
# requires GDAL >= 3.6
edl_set_token()
library(stars)
url <- "https://data.lpdaac.earthdatacloud.nasa.gov/lp-prod-protected/HLSL30.020/HLS.L30.T56JKT.2023246T235950.v2.0/HLS.L30.T56JKT.2023246T235950.v2.0.SAA.tif"
x <- read_stars(paste0("/vsicurl/",url))
plot(x)
# Helper functions for dealing with nasa earthdata login
# Requires GDAL >= 3.6
#
edl_set_token <- function (username = Sys.getenv("EARTHDATA_USER"),
password = Sys.getenv("EARTHDATA_PASSWORD"),
token_number = 1
) {
base <- 'https://urs.earthdata.nasa.gov'
list_tokens <- "/api/users/tokens"
pw <- openssl::base64_encode(paste0(username, ":", password))
resp <- httr::GET(paste0(base,list_tokens),
httr::add_headers(Authorization= paste("Basic", pw)))
p <- httr::content(resp, "parsed")[[token_number]]
if(is.null(p$access_token)) {
request_token <- "/api/users/token"
resp <- httr::GET(paste0(base,request_token),
httr::add_headers(Authorization= paste("Basic", pw)))
p <- httr::content(resp, "parsed")
}
header = paste("Authorization: Bearer", p$access_token)
Sys.setenv("GDAL_HTTP_HEADERS"=header)
invisible(header)
}
# alias
ed_set_token <- edl_set_token
edl_download <- function(x, header = edl_set_token()) {
dest <- basename(x)
bearer <- gsub("Authorization: ", "", header)
httr::GET(x, httr::write_disk(dest, overwrite=TRUE),
httr::add_headers(Authorization = bearer))
#download.file(x, dest, header = list(Authorization = bearer))
dest
}
edl_stac_urls <- function(items, assets = "data") {
purrr::map(items$features, list("assets")) |>
purrr::map(list(assets)) |>
purrr::map_chr("href")
}
@yuvipanda
Copy link

@wildintellect nsidc/earthaccess#188 has a lot of investigative work on this as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment