<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic 'NoneType' object has no attribute 'utf_8_decode' from generateToken in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/nonetype-object-has-no-attribute-utf-8-decode-from/m-p/1019168#M59609</link>
    <description>&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;There are intermittent issues with the function used to get a token for the REST service where the user can get an error:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;'NoneType' object has no attribute 'utf_8_decode'&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the function:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# 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()&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the call to this function in a try except block:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;try:
    token = get_token(username, password, protocol, arcgis_server_url)
except Exception as e:
    output_msg = str(e)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;By using debug print statements, I found that it fails on the request post&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;response = requests.post(token_url, data=params, headers=headers)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example parameters:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;username: username
password: password
protocol = https://
arcgis_server_url = server-name.com/server&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;I have also verified that regardless of whether or not get_token fails, the input parameters are the same.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;I have found that sometimes when the issue starts occurring, I can temporarily fix the issue by restarting the geoprocessing service. I am using&amp;nbsp;&lt;SPAN&gt;ArcGIS&amp;nbsp;Server:&amp;nbsp;10.5.1 with&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;Python&amp;nbsp;2.7.16&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Has anyone run into this problem?&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 22 Jan 2021 17:11:29 GMT</pubDate>
    <dc:creator>VincentLantaca</dc:creator>
    <dc:date>2021-01-22T17:11:29Z</dc:date>
    <item>
      <title>'NoneType' object has no attribute 'utf_8_decode' from generateToken</title>
      <link>https://community.esri.com/t5/python-questions/nonetype-object-has-no-attribute-utf-8-decode-from/m-p/1019168#M59609</link>
      <description>&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;There are intermittent issues with the function used to get a token for the REST service where the user can get an error:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;'NoneType' object has no attribute 'utf_8_decode'&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the function:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# 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()&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the call to this function in a try except block:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;try:
    token = get_token(username, password, protocol, arcgis_server_url)
except Exception as e:
    output_msg = str(e)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;By using debug print statements, I found that it fails on the request post&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;response = requests.post(token_url, data=params, headers=headers)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example parameters:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;username: username
password: password
protocol = https://
arcgis_server_url = server-name.com/server&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;I have also verified that regardless of whether or not get_token fails, the input parameters are the same.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;I have found that sometimes when the issue starts occurring, I can temporarily fix the issue by restarting the geoprocessing service. I am using&amp;nbsp;&lt;SPAN&gt;ArcGIS&amp;nbsp;Server:&amp;nbsp;10.5.1 with&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;Python&amp;nbsp;2.7.16&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Has anyone run into this problem?&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jan 2021 17:11:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nonetype-object-has-no-attribute-utf-8-decode-from/m-p/1019168#M59609</guid>
      <dc:creator>VincentLantaca</dc:creator>
      <dc:date>2021-01-22T17:11:29Z</dc:date>
    </item>
    <item>
      <title>Re: 'NoneType' object has no attribute 'utf_8_decode' from generateToken</title>
      <link>https://community.esri.com/t5/python-questions/nonetype-object-has-no-attribute-utf-8-decode-from/m-p/1019297#M59613</link>
      <description>&lt;P&gt;In this line:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;headers = {'Content-Type': 'application/x-www-form-urlencoded'}&lt;/LI-CODE&gt;&lt;P&gt;you say the content is URL-encoded, but I don't see you actually encoding it anywhere.&lt;/P&gt;&lt;P&gt;Since you are using Python 2.x, see the getToken example at &lt;A href="https://enterprise.arcgis.com/en/server/latest/administer/linux/scripting-languages-and-the-arcgis-rest-api.htm" target="_blank"&gt;Scripting languages and the ArcGIS REST API—ArcGIS Server | Documentation for ArcGIS Enterprise&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jan 2021 21:59:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nonetype-object-has-no-attribute-utf-8-decode-from/m-p/1019297#M59613</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2021-01-22T21:59:36Z</dc:date>
    </item>
    <item>
      <title>Re: 'NoneType' object has no attribute 'utf_8_decode' from generateToken</title>
      <link>https://community.esri.com/t5/python-questions/nonetype-object-has-no-attribute-utf-8-decode-from/m-p/1019706#M59624</link>
      <description>&lt;P&gt;If I'm not mistaken, requests will automatically convert the dictionary:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;A href="https://requests.kennethreitz.org/en/master/user/quickstart/#more-complicated-post-requests" target="_blank"&gt;https://requests.kennethreitz.org/en/master/user/quickstart/#more-complicated-post-requests&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jan 2021 17:33:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nonetype-object-has-no-attribute-utf-8-decode-from/m-p/1019706#M59624</guid>
      <dc:creator>VincentLantaca</dc:creator>
      <dc:date>2021-01-25T17:33:28Z</dc:date>
    </item>
    <item>
      <title>Re: 'NoneType' object has no attribute 'utf_8_decode' from generateToken</title>
      <link>https://community.esri.com/t5/python-questions/nonetype-object-has-no-attribute-utf-8-decode-from/m-p/1019765#M59625</link>
      <description>&lt;P&gt;Testing with httplib and urllib will eliminate that automagical conversion as a source of the problem.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jan 2021 19:17:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nonetype-object-has-no-attribute-utf-8-decode-from/m-p/1019765#M59625</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2021-01-25T19:17:13Z</dc:date>
    </item>
    <item>
      <title>Re: 'NoneType' object has no attribute 'utf_8_decode' from generateToken</title>
      <link>https://community.esri.com/t5/python-questions/nonetype-object-has-no-attribute-utf-8-decode-from/m-p/1020273#M59643</link>
      <description>&lt;P&gt;I've tried adding:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;params = urllib.urlencode(params)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;before the the call to request.post and I still ran into the error&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jan 2021 22:49:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nonetype-object-has-no-attribute-utf-8-decode-from/m-p/1020273#M59643</guid>
      <dc:creator>VincentLantaca</dc:creator>
      <dc:date>2021-01-26T22:49:44Z</dc:date>
    </item>
    <item>
      <title>Re: 'NoneType' object has no attribute 'utf_8_decode' from generateToken</title>
      <link>https://community.esri.com/t5/python-questions/nonetype-object-has-no-attribute-utf-8-decode-from/m-p/1020317#M59644</link>
      <description>&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jan 2021 00:32:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nonetype-object-has-no-attribute-utf-8-decode-from/m-p/1020317#M59644</guid>
      <dc:creator>VincentLantaca</dc:creator>
      <dc:date>2021-01-27T00:32:51Z</dc:date>
    </item>
  </channel>
</rss>

