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
I have also verified that regardless of whether or not get_token fails, the input parameters are the same.
I have found that sometimes when the issue starts occurring, I can temporarily fix the issue by restarting the geoprocessing service. I am using ArcGIS Server: 10.5.1 with Python 2.7.16
Has anyone run into this problem?