Portal Admin Machines REST

1985
14
Jump to solution
12-03-2020 07:00 PM
DeanHowell1
Occasional Contributor III

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? 

 

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
DavidPike
MVP Frequent Contributor

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)

View solution in original post

14 Replies
DavidPike
MVP Frequent Contributor

would you want to do this with requests or the rest api for python?

0 Kudos
DeanHowell1
Occasional Contributor III

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?


 

0 Kudos
DavidPike
MVP Frequent Contributor

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?

0 Kudos
DeanHowell1
Occasional Contributor III

Yes exactly. We are stuck on how to create and pass the relevant credentials through to the rest end point 

0 Kudos
DavidPike
MVP Frequent Contributor

I think the health check doesn't require a toke  give the url a try.

0 Kudos
DeanHowell1
Occasional Contributor III

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

0 Kudos
DavidPike
MVP Frequent Contributor

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)
DavidPike
MVP Frequent Contributor

then any url GETs will just be appended with:

"&token=" + token

DeanHowell1
Occasional Contributor III

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?

0 Kudos