How to generate token for ArcGIS Online/Python

17610
4
04-23-2015 08:04 AM
GeoffreyWest
Occasional Contributor III

I would like to consume hosted features from ArcGIS Online into a local file geodatabase.  I am consistently receving a 498 error.  I am generating a token but when passing into ArcGIS server it is invalid.  A couple of questions.

a.) Do I need to use my organization url, i.e. myorg.maps.arcgis.com for my referrer?

b.) When I am creating a replica; should this data be hosted in arcgis.com?

I am making some changes to the script  linked below which I found on github that uses urllib2, it doesn't work and isn't as elegant as the Requests.  And for my purposes I do not need to add attachments to the database, just consume a table locally.

Python script to pull hosted features with attachments into a local file geodatabase. See ReadMe bel...

Here are my script and results

url = 'https://www.arcgis.com/sharing/generateToken'
payload  = {'username' : 'myUser,
'password' : 'myPass!',
'referer' : 'www.arcgis.com',
'f' : 'json' }

r = requests.post(url, data=payload)
token =json.loads (r.text)

aToken = token['token']

print aToken

# Create Replica ###
# Update service url HERE ###
url2 = 'http://myIP:6080/arcgis/rest/services/FeatureMapService/FeatureServer/CreateReplica'
# #
payload2 = {'f' : 'json',
'layers' : '1',
'returnAttachments' : 'false',
'token' : token }


r2 = requests.post(url2, data=payload2)
repLoad = json.loads (r2.text)


print repLoad
C:\Python27\ArcGIS10.2\python.exe C:/Users/GIS/Documents/ArcGIS/timequery.py
lOg8435-zLN8aeWknNveul8EzYlmbKb2h5GXVHCazEEbs0VgvyKH-0rr-iZ3RNwN_HP0BfykxtuzgZaw_Bm8LMZoOoJ9KtydsLze2mEX5qcFKBKltW_goPIH9w43jvgtCqHGAbotdyGMTAZZ217QEVTNqnW74rgBtjf1eB7XFMUb1miuJbU2YhPDnpbQB1S-9alNOrp6loXtBzJFEN-oAA..
{u'error': {u'message': u'Invalid Token', u'code': 498, u'details': []}}
4 Replies
BlakeTerhune
MVP Regular Contributor

I haven't done this, but maybe you can find something useful in this Esri Blog post about ARCREST.

Spatialize Your Water Utility Data with ArcGIS | ArcGIS Blog

0 Kudos
PF1
by
Occasional Contributor II

How is your arcgis servers security configured?  Are you federated with your arcgis.com solution?  What is your user and role store? 

0 Kudos
GeoffreyWest
Occasional Contributor III

I am an admin user, this solution does not seem very viable as the table has to be hosted on ArcGIS Server, I would like to host a table in ArcGIS Online and consume it to a  local file geodatabase with python.

0 Kudos
PF1
by
Occasional Contributor II

Hi Geoffrey,

I think you are trying to use your ArcGIS Online (AGOL) token to authorize your access to your ArcGIS Server (AGS).  I think you need to have your AGS federated with AGOL in order for this to work. 

If AGS is not federated with AGOL, then you need to acquire an AGS token and use that for access to web-services hosted with AGS.  This also assumes your AGS 'site' is setup to use ArcGIS Tokens for its authentication tier which is the default settings when deployed. 

If you have switched to a 'web-tier' authentication scheme, then you do not require tokens any more but rather use the authentication mechanisms that your web-adaptor deployment supports such as:

  • HTTP Basic
  • HTTP Digest
  • Integrated Windows Authentication (IWA) Using Kerberos or NTLM

Take a look at this thread and see if it helps shed some light: Re: Federating an ArcGIS Server site with your portal