Basic Authentication#

Many servers require authentication. PyDAP includes an extension mechanism so that it can interact with different kinds of authentication systems.

In this short tutorial notebook, we will cover basic EarthDatalogin (EDL) authentication with

  1. username/password.

  2. tokens

making use of Python’s Requests HTTP library to create a session. Of the two approaches above, token authentication will be used and generally recommended thoughout the documentation.

These two types of authentications will be used to access the same dataset, both on OPeNDAP’s Hyrax server. A similar approach can be used to access data on a different server.

Note

For a broader introduction to authentication, check the OPeNDAP documentation on Authentication for DAP Clients, and Client Authentication With EDLTokens.

import requests
from pydap.client import open_url
dataset_url = "https://opendap.earthdata.nasa.gov/providers/POCLOUD/collections/ECCO%20Ocean%20Temperature%20and%20Salinity%20-%20Monthly%20Mean%200.5%20Degree%20(Version%204%20Release%204)/granules/OCEAN_TEMPERATURE_SALINITY_mon_mean_2017-12_ECCO_V4r4_latlon_0p50deg"

username / password approach#

from pydap.cas.urs import setup_session

username = "YourUserName" # Earthdata URS Login Get account here https://urs.earthdata.nasa.gov/
password = "YourPassword"

hyrax_url = 'https://opendap.earthdata.nasa.gov/collections/C2532426483-ORNL_CLOUD/granules/'
session = setup_session(username, password, check_url=hyrax_url)

Now, we open the remote dataset#

ds = open_url(dataset_url, session=session, protocol='dap4')
ds.tree()
.OCEAN_TEMPERATURE_SALINITY_mon_mean_2017-12_ECCO_V4r4_latlon_0p50deg.nc
├──SALT
├──THETA
├──Z_bnds
├──latitude_bnds
├──time_bnds
├──longitude_bnds
├──Z
├──latitude
├──longitude
├──nv
└──time

Note

No data has been downloaded yet!

Token Approach#

This is the approach that will largely will be used throughout the documentation.

edl_token = "YourToken"

auth_hdr="Bearer " + edl_token

# pass Token Authorization to a new Session.

my_session = requests.Session()
my_session.headers={"Authorization": auth_hdr}
ds = open_url(dataset_url, session=my_session, protocol='dap4')
ds.tree()
.OCEAN_TEMPERATURE_SALINITY_mon_mean_2017-12_ECCO_V4r4_latlon_0p50deg.nc
├──SALT
├──THETA
├──Z_bnds
├──latitude_bnds
├──time_bnds
├──longitude_bnds
├──Z
├──latitude
├──longitude
├──nv
└──time

Note

Similarly, no data has been downloaded yet!