Python script to replicate hosted feature service on ArcGIS Online including attachments.

6015
10
Jump to solution
03-28-2016 03:13 PM
JeffWard
Occasional Contributor III

I am trying to get this script to work for an application for one of our departments.  If you look at the comment by stuartalexandersanders in the GitHub link - that is the same error I am getting.  I'm hoping Alex Sanders​ is the same person and has fixed it?

gtUrl = 'https://www.arcgis.com/sharing/rest/generateToken'
gtValues = {'username' : 'MyUsername',
  'password' : 'MyPassword',
  'referer' : 'http://www.arcgis.com',
  'f' : 'json' }
gtData = urllib.urlencode(gtValues)
gtRequest = urllib2.Request(gtUrl, gtData)
gtResponse = urllib2.urlopen(gtRequest)
gtJson = json.load(gtResponse)
token = gtJson['token']

### Create Replica ###
### Update service url HERE ###
crUrl = 'http://services2.arcgis.com/MyServices/arcgis/rest/services/MyServicName/FeatureServer/createReplica'

That code works and I am able to get a token for the feature service.

This next block is where I am having my trouble -

crValues = {'f' : 'json',
  'layers' : '0',
  'returnAttachments' : 'true',
  'token' : token }
crData = urllib.urlencode(crValues)
crRequest = urllib2.Request(crUrl, crData)
crResponse = urllib2.urlopen(crRequest)
crJson = json.load(crResponse)
replicaUrl = crJson['URL']
urllib.urlretrieve(replicaUrl, 'myLayer.json')

The error I get is from the urlretrieve call.  myLayer.json is not a json file but an html file that says "Token Required".  The error text in python is:

Any ideas why this would happen?  Apparently it was working for Alex before the update to ArcGIS Online earlier this month.

Thanks,

Jeff

Jeff Ward
Summit County, Utah
0 Kudos
10 Replies
AlexSanders
Occasional Contributor

ESRI has pointed me to this script that pulls down a fgd from a AGO hosted service.  It seems to be working for me developer-support/createReplicaAndDownload.py at master · Esri/developer-support · GitHub