We need to be able to query the REST end point for the portal machines to determine the primary and standby machines but not sure what the syntax needs to be for accessing it outside the portal administrator.
The REST end point is https://admin.portal...../arcgis/portaladmin/machines?f=pjson which works when I am in the portal admin but how do we add this to an external application with credentials / token?
Solved! Go to Solution.
ah gotcha,
So the initial query is to get the machine names:
https://machine.domain.com/webadaptor/portaladmin/machines
then the status for each machine:
https://machine.domain.com/webadaptor/portaladmin/machines/status/MACHINE.DOMAIN.COM?f=json
the token is generated with:
import requests
portalUrl = "https://domain/portal"
username = 'username'
password = "password"
def generateToken(username, password, portalUrl):
# Retrieves a token to be used with API requests.
headers = {'content-type': 'application/x-www-form-urlencoded'}
parameters = {'username': username,
'password': password,
'client': 'referer',
'referer': portalUrl,
'expiration': 60,
'f': 'json'}
url = portalUrl + '/sharing/rest/generateToken?'
response = requests.post(url, data=parameters, headers=headers)
try:
jsonResponse = response.json()
if 'token' in jsonResponse:
return jsonResponse['token']
elif 'error' in jsonResponse:
print (jsonResponse['error']['message'])
for detail in jsonResponse['error']['details']:
print (detail)
except ValueError:
print('An unspecified error occurred.')
print(ValueError)
token = generateToken(username, password, portalUrl)
print(token)
would you want to do this with requests or the rest api for python?
Thanks @DavidPike we are looking to do as a request from an external script that needs to shut down the portal in a specific order depending on the machine status in HA
@DavidPike wrote:would you want to do this with requests or the rest api for python?
gotcha, so you'd want to do a health check via the REST API Health Check—ArcGIS REST API | ArcGIS for Developers but need to know how to generate and attach the token for access?
Yes exactly. We are stuck on how to create and pass the relevant credentials through to the rest end point
I think the health check doesn't require a toke give the url a try.
Thanks again @DavidPike, yes the healthCheck works without a token but it is the machines that I need to query but have just found the help page for the machines which might give some greater insight https://developers.arcgis.com/rest/enterprise-administration/portal/status.htm
ah gotcha,
So the initial query is to get the machine names:
https://machine.domain.com/webadaptor/portaladmin/machines
then the status for each machine:
https://machine.domain.com/webadaptor/portaladmin/machines/status/MACHINE.DOMAIN.COM?f=json
the token is generated with:
import requests
portalUrl = "https://domain/portal"
username = 'username'
password = "password"
def generateToken(username, password, portalUrl):
# Retrieves a token to be used with API requests.
headers = {'content-type': 'application/x-www-form-urlencoded'}
parameters = {'username': username,
'password': password,
'client': 'referer',
'referer': portalUrl,
'expiration': 60,
'f': 'json'}
url = portalUrl + '/sharing/rest/generateToken?'
response = requests.post(url, data=parameters, headers=headers)
try:
jsonResponse = response.json()
if 'token' in jsonResponse:
return jsonResponse['token']
elif 'error' in jsonResponse:
print (jsonResponse['error']['message'])
for detail in jsonResponse['error']['details']:
print (detail)
except ValueError:
print('An unspecified error occurred.')
print(ValueError)
token = generateToken(username, password, portalUrl)
print(token)
then any url GETs will just be appended with:
"&token=" + token
Thanks @DavidPike once again very much appreciated.
Does the token need to be generated for the specific page / URL that you are trying to access or just for the portal?