Add support for OAuth2 authentication to the HTTP downloader
Closed this issue · 0 comments
aperrin66 commented
The HTTP downloader should support OAuth2 authentication.
The type of authentication can be set in the provider_settings.yml
file.
Here is a chunk of code which downloads a file from Creodias with OAuth2 authentication:
import requests
from oauthlib.oauth2 import LegacyApplicationClient
from requests_oauthlib import OAuth2Session, OAuth2
url = 'https://zipper.creodias.eu/download/e25d0589-c0af-53c1-8ec1-2995bbcb64c6'
token_url = 'https://auth.creodias.eu/auth/realms/DIAS/protocol/openid-connect/token'
client_id = 'CLOUDFERRO_PUBLIC'
username = 'user'
password = 'passwd'
client = LegacyApplicationClient(client_id=client_id)
token = OAuth2Session(client=client).fetch_token(
token_url=token_url,
username=username,
password=password,
client_id=client_id,
)
oauth2 = OAuth2(client_id=client_id, client=client, token=token)
r = requests.get(url, auth=oauth2, stream=True)
with open('/tmp/test_creodias.zip', 'bw') as f:
for chunk in r.iter_content(chunk_size=1024 * 1024):
f.write(chunk)
r.close()