Select to view content in your preferred language

Python Script Using ArcGIS Online Credentials

6252
14
Jump to solution
08-09-2016 06:41 AM
ISP_graynic
Frequent Contributor


I have a basic python script that takes a hosted feature service from ArcGIS Online and converts it to an excel (.xls) file.  The only problem is, I need to sign-in to ArcGIS Online within ArcMap to get the script to run, since the feature service is only shared with my organization.  Is there any way to store my credentials within the script so that it will run on a daily basis without me having to sign-in on ArcMap every time?  Any help is greatly appreciated.  Thank you!

0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

I don't believe you will be able to export a feature service to an excel table using the Table to Excel tool.  What you could do is export the feature service to a local File Geodatabase and then export the feature class to excel.  Here is a tool you can use to export the feature service.

View solution in original post

14 Replies
JamesCrandall
MVP Frequent Contributor

We are not running in ArcGIS Desktop, but we execute a python script from Windows Task Scheduler that updates a hosted Feature Service on our AGOL org account.  I suppose you'd just need to grab the token by making calls against the REST API.

It'd help to see what your script is doing.

We used the majority of this https://blogs.esri.com/esri/arcgis/2014/01/24/updating-your-hosted-feature-service-for-10-2/ article to implement.  Again, it's just authenticating directly with the ArcGIS.com REST API.

ISP_graynic
Frequent Contributor

Thank you for the help James.  My script is a real basic model I exported from Model Builder as a python script.

# Import arcpy module

import arcpy

# Local variables:

SpecialEvents = "SpecialEvents\\SpecialEvents"

SE_Test_xls = "I:\\Documents\\SE_Test.xls"

# Process: Table To Excel

arcpy.TableToExcel_conversion(SpecialEvents, SE_Test_xls, "NAME", "CODE")

I have replied to Jake's response with what I am trying based on his comment, but don't believe I am doing it right.

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Hi Nicholas,

You can generate a token using the following script:

import urllib, urllib2, json

username = "abcd"
password = "1234"

tokenURL = 'https://www.arcgis.com/sharing/rest/generateToken'
params = {'f': 'pjson', 'username': username, 'password': password, 'referer': 'http://www.arcgis.com'}
req = urllib2.Request(tokenURL, urllib.urlencode(params))
response = urllib2.urlopen(req)
data = json.load(response)
token = data['token']

You can then append the token onto the feature service URL.  Ex:

damageAssessmentURL = 'http://services.arcgis.com/Fz6BBJUji5USDM/arcgis/rest/services/Damage_Assessment/FeatureServer/0/que...'
where = "PropertyType = 'Single Dwelling Houses'"
params = {'f': 'pjson', 'where': where, 'returnCountOnly': 'true', 'token': token}
req = urllib2.Request(damageAssessmentURL, urllib.urlencode(params))
response = urllib2.urlopen(req)
data = json.load(response)
ISP_graynic
Frequent Contributor

Here is the simple code below that I am trying to generate and add the token too.

import arcpy,

SE_Test_xls = "I:\\Documents\\SE_Test.xls"

# Process: Table To Excel

arcpy.TableToExcel_conversion(SpecialEvents, SE_Test_xls, "NAME", "CODE")

0 Kudos
JakeSkinner
Esri Esteemed Contributor

I don't believe you will be able to export a feature service to an excel table using the Table to Excel tool.  What you could do is export the feature service to a local File Geodatabase and then export the feature class to excel.  Here is a tool you can use to export the feature service.

ISP_graynic
Frequent Contributor

Thank you Jake. Is an ArcGIS Standard License needed for this script to run?

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Actually, only if you are downloading attachments.  I'll need to update this tool to reflect this.  You can do this by opening up the .py file and deleting lines 4-9.  It should then work as long as you are not extracting attachments.

ISP_graynic
Frequent Contributor

Awesome.  It still said it was successful but was aborting because it wasn't a Standard License.  When I took those lines out it works perfectly.  Thank you so much for the help.  This tool is great!!!

0 Kudos
ISP_graynic
Frequent Contributor

I have one more question for you Jake.  I got the script to work without downloading attachments.  But when I run it to download attachments, I get the following error.  I checked the feature class and the "GlobalID_Str" gets created, but it sounds like it's having trouble finding it. 

Traceback (most recent call last):

  File "C:\Users\Downloads\TEST Attachments.py", line 194, in <module>

    for row in cursor:

RuntimeError: A column was specified that does not exist.

0 Kudos