-
Notifications
You must be signed in to change notification settings - Fork 0
/
geonetwork-api-authentication.py
32 lines (25 loc) · 1.5 KB
/
geonetwork-api-authentication.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#Get GeoNetwork XSRF-token and session-ID from cookie and add to headers for authentication
import requests
from requests.auth import HTTPBasicAuth
session = requests.Session()
session.auth = HTTPBasicAuth('[username]','[password]') #Replace [username] and [password] with your GeoNetwork username and password
session.post('http://localhost:8080/geonetwork/srv/eng/info?type=me')
session.headers.update({'X-XSRF-TOKEN': session.cookies.get('XSRF-TOKEN')})
session.post('http://localhost:8080/geonetwork/j_spring_security_check')
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-US,en;q=0.9",
"reset": "true",
"username": "[username]", #Replace [username] with your GeoNetwork username
"password": "[password]", #Replace [password] with your GeoNetwork password
"havingXlinkOnly": "false",
"Connection": "keep-alive",
"Cookie": "XSRF-TOKEN="+session.cookies.get('XSRF-TOKEN')+"; JSESSIONID="+session.cookies.get('JSESSIONID')+"; serverTime=1657745480260; sessionExpiry=1657745480260",
"X-XSRF-TOKEN": session.cookies.get('XSRF-TOKEN')
}
payload = {}
#GeoNetwork API request with authentication
url_metadata = "http://localhost:8080/geonetwork/srv/api/0.1/site/index" #This API request resets the index and is used here as an example.
r_metadata = requests.request("PUT", url_metadata, headers=headers, data=payload)
print("Server response: "+ str(r_metadata))