'NoneType' object has no attribute 'utf_8_decode' from generateToken

681
5
01-22-2021 09:11 AM
VincentLantaca
New Contributor II

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?
 
 

 

0 Kudos
5 Replies
JoshuaBixby
MVP Esteemed Contributor

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

0 Kudos
VincentLantaca
New Contributor II

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.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Testing with httplib and urllib will eliminate that automagical conversion as a source of the problem.

0 Kudos
VincentLantaca
New Contributor II

I've tried adding:

params = urllib.urlencode(params)

 

before the the call to request.post and I still ran into the error

0 Kudos
VincentLantaca
New Contributor II

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.

0 Kudos