Documentation Home

Data Download Script with an EDL Token (Perl)

Script

edl_token_script.pl

Purpose

The following Perl code example demonstrates how to configure a connection to download data from an Earthdata Login enabled server. Note that you will need to a secure way to configure the Earthdata user token.

You can obtain user token using the EDL API, /api/users/find_or_create_token.

Examples

#!/usr/bin/perl

use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Cookies;

print "Enter your Bearer token: ";
chomp(my $token = <STDIN>);
# Variables that need to be set by the user
my @urls    = ("https://n5eil01u.ecs.nsidc.org/MOST/NSIDC-0791.001/2000.03.01/NSIDC-0791_CSS_0.01Deg_WY2001-2023_V01.0.nc.xml",
               "https://n5eil01u.ecs.nsidc.org/MOST/NSIDC-0791.001/2000.03.01/NSIDC-0791_FSS_0.01Deg_WY2001-2023_V01.0.nc.xml"
               );

# Create a cookie jar for session management
my $cookie_file = "$ENV{HOME}/.cookies.txt";

# Variable that does not need to be changed
my $ua = LWP::UserAgent->new(
    cookie_jar  => HTTP::Cookies->new(
            file => $cookie_file,
            ignore_discard => 1,
            autosave => 1),
    ssl_opts => { verify_hostname => 1, SSL_ca_file => '/etc/ssl/cert.pem' }
);

foreach my $url (@urls) {
    my $req = HTTP::Request->new(GET => $url);
    $req->header('Authorization' => "Bearer $token");

    my $res = $ua->request($req);

    if ($res->is_success) {
        # Set filename from URL
        my $filename = $url;
        $filename =~ s{.*/}{};
        open my $fh, '>', $filename or die "Cannot open $filename: $!";
        binmode $fh;
        print $fh $res->content;
        close $fh;
        print "Downloaded $filename\n";
    } else {
        # Handle error here
        warn "HTTP GET error for $url: ", $res->status_line, "\n";
        # If you are seeing 429 (rate limit), we suggest adding retry logic to the script.
    }
}