Tokens and Cookies to call API

106
2
10-21-2020 09:52 AM
Highlighted
New Contributor

Can someone walk me through getting a token or token wrapped in a cookie from an ESRI Portal and then where and how to pass it to the REST API to pull the geojson from a feature layer service?

Reply
0 Kudos
2 Replies
Highlighted
Esri Esteemed Contributor

Here is an example on how to create a token using Python:

import requests, json

# Disable warnings
requests.packages.urllib3.disable_warnings()

portal = 'portal.esri.com'
username = "jskinner@ESRI"
password = "**********"

tokenURL = 'https://{0}:7443/arcgis/sharing/rest/generateToken/'.format(portal)
params = {'f': 'pjson', 'username': username, 'password': password, 'client': 'requestip'}

r = requests.post(tokenURL, data = params, verify=False)
response = json.loads(r.content)
token = response['token']
print(token)
Reply
0 Kudos
Highlighted
Esri Contributor

Jake already gave a good snippet to generate a token from python.

For the next step you need, to send a request to rest-endpoint to retrieve the GeoJSON from a feature layer, this could be done via a query operation with f and token parameters,e.g: 

https://machine.domain.com/webadaptor/rest/services/USAStatesRiversCapitals/FeatureServer/2/query?where=1=1&f=geojson&token=tokenstring

so making an HTTP request to above URL with whatever programing language you're using. 

Reply
0 Kudos