I have a published geoprocessing service that is being used by web app builder. This geoprocessing service makes a call to an ArcGIS REST Service. The code first requests for a token so that it can use the REST service to add features to a feature class.
There are intermittent issues with the function used to get a token for the REST service where the user can get an error:
'NoneType' object has no attribute 'utf_8_decode'
This is the function:
# get access token from the arcgis rest server
def get_token(username, password, protocol, arcgis_server_url):
token_url = "{protocol}{host}".format(
protocol=protocol,
host=arcgis_server_url
) + r'/tokens/generateToken'
params = {
'username': username,
'password': password,
"client": "requestip",
'f': 'json'
}
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
response = requests.post(token_url, data=params, headers=headers)
if response.status_code == 200:
if 'token' in response.json():
return response.json()['token']
else:
error_msg = 'An error occurred while trying to create an access token. Check login info. Aborting.'
arcpy.AddError(error_msg)
arcpy.SetParameterAsText(13, error_msg)
sys.exit()
else:
error_msg = 'An error occurred while trying to get access token. Aborting.'
arcpy.AddError(error_msg)
arcpy.SetParameterAsText(13, error_msg)
sys.exit()
I have the call to this function in a try except block:
try:
token = get_token(username, password, protocol, arcgis_server_url)
except Exception as e:
output_msg = str(e)
By using debug print statements, I found that it fails on the request post
response = requests.post(token_url, data=params, headers=headers)
Example parameters:
username: username
password: password
protocol = https://
arcgis_server_url = server-name.com/server
In this line:
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
you say the content is URL-encoded, but I don't see you actually encoding it anywhere.
Since you are using Python 2.x, see the getToken example at Scripting languages and the ArcGIS REST API—ArcGIS Server | Documentation for ArcGIS Enterprise
If I'm not mistaken, requests will automatically convert the dictionary:
https://requests.kennethreitz.org/en/master/user/quickstart/#more-complicated-post-requests
I will try using httplib and urllib and see if that helps, but it is still unclear why the script fails sometimes and works other times.
Testing with httplib and urllib will eliminate that automagical conversion as a source of the problem.
I've tried adding:
params = urllib.urlencode(params)
before the the call to request.post and I still ran into the error
I am noticing that CPU and Memory usage can be high when this happens, I am still not 100% sure that is the cause though.