FeatureLayer.query() returns RuntimeError: Token Required (Error Code: 499)

1699
4
10-18-2018 09:47 AM
JoeHershman
MVP Regular Contributor

I am trying to query a FeatureLayer.  I can connect to the GIS (using IWA), I validate the connection by checking the User, so I know the GIS object is valid.

Create a FeatureLayer object using a valid Url (the FeatureLayer object is created).  But when I run query I get RuntimeError: Token Required (Error Code: 499).  See full stack trace below.  Pretty simple code

gis = GIS(portal, '', '')  # IWA

# get the webmap to use offline
offline_map_item = gis.content.get(webmap)
offline_webmap = WebMap(offline_map_item)

featureLayer = FeatureLayer(projects_url, gis)
features = featureLayer.query()

The issue is I see no way you would pass a token to this method.  I passed the GIS object when initializing the FeatureLayer and the server is federated so I would think this passes the required tokens.  Can some please help explain how I should be passing a token to this method from an IWA configured portal to a federated server in this call.

Thanks

-Joe

Stack trace:

RuntimeError                              Traceback (most recent call last)
C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\gis\__init__.py in _hydrate(self)
   7577 
-> 7578                 self._refresh()
   7579 

C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\gis\__init__.py in _refresh(self)
   7544         else:
-> 7545             dictdata = self._con.post(self.url, params, token=self._lazy_token)
   7546 

C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\_impl\connection.py in post(self, path, postdata, files, ssl, compress, is_retry, use_ordered_dict, add_token, verify_cert, token, try_json, out_folder, file_name, force_bytes, add_headers)
   1155 
-> 1156                 self._handle_json_error(resp_json['error'], errorcode)
   1157                 return None

C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\_impl\connection.py in _handle_json_error(self, error, errorcode)
   1176         errormessage = errormessage + "\n(Error Code: " + str(errorcode) +")"
-> 1177         raise RuntimeError(errormessage)
   1178 

RuntimeError: Token Required
(Error Code: 499)

During handling of the above exception, another exception occurred:

RuntimeError                              Traceback (most recent call last)
C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\gis\__init__.py in _hydrate(self)
   7586                     self._lazy_token = None
-> 7587                     self._refresh()
   7588 

C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\gis\__init__.py in _refresh(self)
   7544         else:
-> 7545             dictdata = self._con.post(self.url, params, token=self._lazy_token)
   7546 

C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\_impl\connection.py in post(self, path, postdata, files, ssl, compress, is_retry, use_ordered_dict, add_token, verify_cert, token, try_json, out_folder, file_name, force_bytes, add_headers)
   1155 
-> 1156                 self._handle_json_error(resp_json['error'], errorcode)
   1157                 return None

C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\_impl\connection.py in _handle_json_error(self, error, errorcode)
   1176         errormessage = errormessage + "\n(Error Code: " + str(errorcode) +")"
-> 1177         raise RuntimeError(errormessage)
   1178 

RuntimeError: Token Required
(Error Code: 499)

During handling of the above exception, another exception occurred:

RuntimeError                              Traceback (most recent call last)
<ipython-input-12-554803401d1a> in <module>
      2 
      3 featureLayer = FeatureLayer(projects_url, gis)
----> 4 features = featureLayer.query()

C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\features\layer.py in query(self, where, out_fields, time_filter, geometry_filter, return_geometry, return_count_only, return_ids_only, return_distinct_values, return_extent_only, group_by_fields_for_statistics, statistic_filter, result_offset, result_record_count, object_ids, distance, units, max_allowable_offset, out_sr, geometry_precision, gdb_version, order_by_fields, out_statistics, return_z, return_m, multipatch_option, quantization_parameters, return_centroid, return_all_records, result_type, historic_moment, sql_format, return_true_curves, return_exceeded_limit_features, **kwargs)
    567 
    568         params['returnCountOnly'] = True
--> 569         record_count = self._query(url, params)
    570         if 'maxRecordCount' in self.properties:
    571             max_records = self.properties['maxRecordCount']

C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\features\layer.py in _query(self, url, params)
   1194         """ returns results of query """
   1195         result = self._con.post(path=url,
-> 1196                                 postdata=params, token=self._token)
   1197         if 'error' in result:
   1198             raise ValueError(result)

C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\gis\__init__.py in _token(self)
   7606             return self._lazy_token
   7607         else:
-> 7608             self._hydrate()
   7609             return self._lazy_token
   7610 

C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\gis\__init__.py in _hydrate(self)
   7594                         # try token in the provided gis
   7595                         self._lazy_token = self._con.token
-> 7596                         self._refresh()
   7597 
   7598         if err is not None:

C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\gis\__init__.py in _refresh(self)
   7543             dictdata = self._con.get(self.url, params, token=self._lazy_token)
   7544         else:
-> 7545             dictdata = self._con.post(self.url, params, token=self._lazy_token)
   7546 
   7547         self._lazy_properties = PropertyMap(dictdata)

C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\_impl\connection.py in post(self, path, postdata, files, ssl, compress, is_retry, use_ordered_dict, add_token, verify_cert, token, try_json, out_folder, file_name, force_bytes, add_headers)
   1154                                          verify_cert=verify_cert, is_retry=True)
   1155 
-> 1156                 self._handle_json_error(resp_json['error'], errorcode)
   1157                 return None
   1158 

C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\_impl\connection.py in _handle_json_error(self, error, errorcode)
   1175 
   1176         errormessage = errormessage + "\n(Error Code: " + str(errorcode) +")"
-> 1177         raise RuntimeError(errormessage)
   1178 
   1179 class _StrictURLopener(request.FancyURLopener):

RuntimeError: Token Required
(Error Code: 499)
Thanks,
-Joe
0 Kudos
4 Replies
JoeHershman
MVP Regular Contributor

This does work if I get rid of IWA and just use portal authentication.  But I cannot believe we cannot query a FeatureLayer in python if using IWA.  

Thanks,
-Joe
0 Kudos
simoxu
by MVP Regular Contributor
MVP Regular Contributor

Have you solved the problem when using IWA?

0 Kudos
RohitSingh2
Esri Contributor

When using IWA, we don't provide the username and password altogether. The code should be 

gis = GIS(portal)  # IWA

instead of 

gis = GIS(portal, '', '')  # IWA

Can you confirm you're using the first way of connecting using IWA?

0 Kudos
simoxu
by MVP Regular Contributor
MVP Regular Contributor

Hi Rohit,

I have the same issue. As you suggested, I use don't use username and password when connecting to my portal.

Interestingly, I only have this issue with hosted feature services (not shared, but I am the owner with administrator privilege), if the service is shared to everyone or not hosted, no issues.

so what's the possible cause?

Thanks.

0 Kudos