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...
https://enterprise.arcgis.com/en/server/10.8/administer/windows/example-write-properties-of-all-services-to-a-csv-file.htm
https://enterprise.arcgis.com/en/portal/10.8/administer/windows/example-add-members-to-the-portal.htm
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')
try:
#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)
else:
message = 'Error Code: %s \n Message: %s' % (errDict['code'],
errDict['message'])
raise SystemExit(message)
token = responseJSON.get('token')
return token
print('...Connecting to ' + portalUrl)
token = generateToken(username,password, serverUrl)
print('...Adding users ')