generateToken request from Python and dynamic IP addresses generate errors

638
2
Jump to solution
10-23-2020 06:50 AM
Jay_Gregory
Regular Contributor

I'm having some issues with using tokens I generate from a Python script by hitting a public ArcGIS Server's generateToken endpoint.  

Our IT routes traffic in some convoluted ways (which is to say dynamically) such that using a token generated from a generateToken request using 'client':'requestip' in the parameters sometimes will end up with a token denied error.  Basically, if the generateToken request first comes from ip address B, but then the REST request comes from IP address B but uses the token generated from ip address A, I'll get an error.  

I'm curious if there is a way around this somehow without going through IT. I'm unclear how I would use 'client':'referer' in the generateToken request, since this is coming from a Python script being run on a server and not a webapp.  Is this even possible? 

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

Every arcgis.gis.GIS and arcgis.gis.server.Server has an underlying connection object that has an underlying Requests session.  You just need to set the token based on a referrer and then update your Requests session header to reflect it:

>>> from arcgis.gis.server import Server
>>>
>>> kwargs = {
...     'url': ,
...     'username': ,
...     'password': 
... }
>>>
>>> svr = Server(**kwargs)
>>>
>>> svr._con
<arcgis.gis._impl._con._connection.Connection object at 0x00000164C182FD68>
>>>
>>> svr._con._session
<requests.sessions.Session object at 0x00000164C6988128>
>>>
>>> svr._con._session.headers
{'User-Agent': 'Geosaurus/1.8.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Referer': 'http'}
>>>
>>> svr._con._session.headers['referrer'] = r'http://www.whateveryoulike.com/myscript'
>>>
>>> svr._con._session.headers
{'User-Agent': 'Geosaurus/1.8.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Referer': 'http', 'referrer': 'http://www.whateveryoulike.com/myscript'}
>>>
>>>

View solution in original post

2 Replies
JoshuaBixby
MVP Esteemed Contributor

Every arcgis.gis.GIS and arcgis.gis.server.Server has an underlying connection object that has an underlying Requests session.  You just need to set the token based on a referrer and then update your Requests session header to reflect it:

>>> from arcgis.gis.server import Server
>>>
>>> kwargs = {
...     'url': ,
...     'username': ,
...     'password': 
... }
>>>
>>> svr = Server(**kwargs)
>>>
>>> svr._con
<arcgis.gis._impl._con._connection.Connection object at 0x00000164C182FD68>
>>>
>>> svr._con._session
<requests.sessions.Session object at 0x00000164C6988128>
>>>
>>> svr._con._session.headers
{'User-Agent': 'Geosaurus/1.8.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Referer': 'http'}
>>>
>>> svr._con._session.headers['referrer'] = r'http://www.whateveryoulike.com/myscript'
>>>
>>> svr._con._session.headers
{'User-Agent': 'Geosaurus/1.8.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Referer': 'http', 'referrer': 'http://www.whateveryoulike.com/myscript'}
>>>
>>>
Jay_Gregory
Regular Contributor

Thank you - I'm not using the Python API in this case, but just using requests directly.  And yes, it was easy to add a referer header to my request directly and everything seemed to work nicely!

headers = {'referer':'https://myurl/myscript'}

data = requests.post(url, data, headers=headers)

0 Kudos