Using a token to access AGOL data

1040
1
06-20-2018 11:41 AM
jameshickey
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('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

0 Kudos
1 Reply
GeofyAdmin
New Contributor III

James,

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 arcgis.com 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