Select to view content in your preferred language

Using a token to access AGOL data

06-20-2018 11:41 AM
Occasional Contributor

I would like to run a python script to access and update a table that is part of a feature service in AGOL. 

I am successful in retrieving a token but I am having a hard time finding any documentation on how to use that token to access the data in AGOL. 

The examples that I have seen only use a username/password combination to access data. This does not work to access an endpoint to a specific layer.

Any help or an example would be much appreciated. I am essentially  lost at line 21...

from arcgis.gis import GIS
import requests
from arcgis.features import FeatureLayerCollection
client_id = "My App ID from AGOL"
client_secret = "My Client Secret from App in AGOL"
def get_token(client_id, client_secret):
 params = {'client_id': client_id,
 'client_secret': client_secret,
 'grant_type': "client_credentials"
 request = requests.get('', params=params)
 response = request.json()
 token = response["access_token"]
 return token
token = get_token(client_id, client_secret)
token_params = {
 'f': 'json',
 'token': token 
service_url = ""
#data =, params=token_params)
gis = GIS(service_url + token)
lyr_result ='title:StormGateInfo')



0 Kudos
1 Reply
New Contributor III


this is already solved with ArcGIS for Python version >1.4.

Here is my answer to a similar questions:

"Actually David and all, this has already been resolved starting with ArcGIS for Python version 1.4. Make sure you have updated to this or latest 1.5 version.

The GIS class has been extended to take a token as part of the kwargs optional keyword arguments.

Code below assumes connecting to and uses a token (it will also connect to your organizational account in agol).

AGOLConnection = GIS(token=agolToken)

if you wanted to connect to your own portal then provide the url parameter."

0 Kudos