AnsweredAssumed Answered

Using a token to access AGOL data

Question asked by dogdaynoon on Jun 20, 2018
Latest reply on Sep 18, 2018 by geofyadmin

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('https://www.arcgis.com/sharing/oauth2/token', params=params)
response = request.json()
token = response["access_token"]
return token
token = get_token(client_id, client_secret)
print(token)
token_params = {
'f': 'json',
'token': token
}
service_url = "https://services2.arcgis.com/FFD8998dfadf024/arcgis/rest/services/WaterBoxTesting/FeatureServer/1"
#data = requests.post(service_url, params=token_params)
#print(data.json())
gis = GIS(service_url + token)
lyr_result = gis.content.search('title:StormGateInfo')
lyr_result[0]

 

Thanks, 

James

Outcomes