JSON responses for use with the Examples of ESRI interaction with Enterprise server information

02-23-2024 10:29 AM
Labels (3)
New Contributor III

I cannot seem to make any of the examples work on our enterprise setup.  I have added what I think was necessary to get past the parsing and the client and the request.  Formatting of things from str to bytes etc has been employed to satisfy my steps to the next error.

These sample/examples are a little "outdated" me thinks...  



The included code used the add member example as a starting point to work out communication between all parts...  

I am stumped here now...   like I said, each time I was able to reformat responses to move to the next part of the code, I bomb out again on something else...  

Here is the error...

File "C:\SGT\ESRI\Python\ArcGIS Enterprise\AddUserToEntPortal-fumblethru.py", line 58, in generateToken
if responseJSON.has_key('error'):
AttributeError: 'dict' object has no attribute 'has_key'

Anyone have a working example of this...  please.. 🙂


#!/usr/bin/env python
# Requires Python 2.7+

# Demonstrates how to add users to the ArcGIS Enterprise portal in bulk

# For Http calls
import urllib3, urllib, json
import urllib.parse
import urllib.response
from urllib.request import urlopen

from notebook.notebookapp import raw_input

# This function connects to the portal and adds members to it from a collection
# def createUsers(username,password, portalUrl, provider,userParamsQ):
# ## these are the tinkerbell-az server credentials: DEVELOPMENT server
username = "siteadmin"
password = "CxrbWeFjT21udxf2sgsdfg$2mDWGQyPu2YyfdgxWSqaA"
portalUrl = "https://tinkerbell-az.ci.janesville.wi.us/devportal"
serverUrl = "https://tinkerbell-az.ci.janesville.wi.us/devserver/admin"

# This function gets a token from the portal
#def generateToken(username, password, serverUrl, json_obj=None):
def generateToken(username, password, serverUrl):
parameters = urllib.parse.urlencode({'username' : username,
'password' : password,
'client' : 'referer',
'referer': portalUrl,
'expiration': 600,
'f' : 'json'})
pmans = parameters.encode('utf-8')
#response = urllib3.urlopen(portalUrl + '/sharing/rest/generateToken?', parameters).read()
response = urllib.request.urlopen(serverUrl + '/generateToken?', pmans)
#response = urllib.request.urlopen(serverUrl + '/generateToken?', pmans).read()
responsestring = response.read().decode('utf-8')
json_obj = json.loads(responsestring)
#print('this is the response :' + json_obj)

except Exception as e:
#raise SystemExit( 'Unable to open the url %s/sharing/rest/generateToken' % (portalUrl))
raise SystemExit('Unable to open the url %s/' % (serverUrl))
# responseJSON = json.loads(response.strip(' \t\n\r'))

responseJSON = json.loads(responsestring)

#content = json.loads(r.decode('utf-8').replace('\n', ''))

# Log results
if responseJSON.has_key('error'):
errDict = responseJSON['error']
if int(errDict['code'])==498:
message = 'Token Expired. Getting new token... '
token = generateToken(username,password, serverUrl)
message = 'Error Code: %s \n Message: %s' % (errDict['code'],
raise SystemExit(message)
token = responseJSON.get('token')
return token

print('...Connecting to ' + portalUrl)
token = generateToken(username,password, serverUrl)
print('...Adding users ')

6 Replies
Esri Contributor

I'd like to clarify first, are you trying to get the information of all services using rest service? Have you tried using ArcGIS API for Python?

I have a git repo that lists all services using ArcGIS API for Python. It needs an administrator level. The output is an Excel that is based on my specifications.

0 Kudos
New Contributor III

Good Day,

Unless I am mistaken..  this code is using the API. ??  

My hope is to get the appropriate connection process to interact with the Enterprise for any information I need.  I do want to get a list of services, users, groups, permissions... etc  The Excel sheet is a bonus... 🙂  want to read and write as needed.  I just cannot get a clean connnection.  syntax encoding/parsing etc etc ..   



0 Kudos
New Contributor III

I made this work..  

Esri Contributor

That's great! 

I never used a token, thanks in advance too that I can elaborate your works with mine in the future. Here is a previous of my work, if you want to look. I create a report from the portal and server regarding the service and items that have been published by all users. I have a different approach when signing in to the enterprise.  The enterprise I am using is 11.2 and ArcGIS pro 3.2.

Hope it helps you. 

New Contributor III

I looked at this and using PyCharm, I was able to get things running and reporting.  I discovered that I did not have any ogc services running. The report service .py kept erroring out.  I commented the code for the

df_ogc = pd.concat(list_ogc_service).reset_index().drop(columns=['index'])


df_ogc.to_excel(writer, sheet_name='OGC Server')

then I got it to run.  When I get some OGC services running I will uncomment those lines.

Thank you again.  The reports are useful.  🙂


P.S. I will still pursue a remedy for my json stuff...  

0 Kudos
New Contributor III

Thank you..  I will take a look at this.  

Best Regards.


0 Kudos