POST
|
OK, I finally got a solution to work (posted below). The code is different from what Kevin posted above but the key to making this work was to change the value for the SERVER variable (or REFER in Kevin's example) from something like "https://myGovWebsite.gov" to just "myGovWebsite.gov" (no http prefix). Sorry about the missing indents for the Python code below. import arcpy, json, urllib, httplib USERNAME = "____" PASSWORD = "_____" SERVERNAME = "FishAndWildlifeURL.gov"#omit the "https://" prefix for this to work PORT = "6443" def getToken(username, password, serverName, serverPort): tokenURL = "https://FishAndWildlifeURL:6443/arcgis/admin/generateToken" params = urllib.urlencode({'username': username, 'password': password,'client': 'requestip', 'f': 'json'}) headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"} # Connect to URL and post parameters httpConn = httplib.HTTPSConnection(serverName, serverPort) httpConn.request("POST", tokenURL, params, headers) # Read response response = httpConn.getresponse() print response.status if (response.status != 200): httpConn.close() print "Error while fetching tokens from admin URL. Please check the URL and try again." return else: data = response.read() httpConn.close() # Check that data returned is not an error object if not assertJsonSuccess(data): return # Extract the token from it token = json.loads(data) return token['token'] ###End getToken function # A function that checks that the input JSON object is not an error object. def assertJsonSuccess(data): obj = json.loads(data) if 'status' in obj and obj['status'] == "error": print "Error: JSON object returns an error. " + str(obj) return False else: return True ### End jsonSuccess function #Query for service, with URL, where clause, fields and token baseURL= "https://FishAndWildlifeURL:6443/arcgis/rest/services/myFolder/myObs/FeatureServer/0/query" where = '1=1' fields = '*' token = getToken(USERNAME, PASSWORD, SERVERNAME, PORT) query = "?where={}&outFields={}&returnGeometry=true&f=json&token={}".format(where, fields, token) fsURL = baseURL + query fs = arcpy.FeatureSet() fs.load(fsURL) arcpy.CopyFeatures_management(fs, r"C:\Default.gdb\temp_obs")
... View more
11-25-2013
11:40 AM
|
2
|
1
|
1267
|
POST
|
Thanks for the code Kevin. Unfortunately, I'm still having problems. When I use your token code, the token is being generated but I still can't access the feature service. If I message the URL + Query I get the message below showing the token (URL has been partially rewritten): "https://FishAndWildlifeURL.gov/arcgis/rest/services/Wildlife/WildlifePtObs/FeatureServer/0/query?where=1=1&outFields=&returnGeometry=true&f=json&token=AgPfmAcCANy32hXn9QGsOGV2K0ObDiqw8ZQ0tBIV3Ao1HAOEJG-MIJtZpdWHBkZ2Y9eab_1oGdwqyNe9jaoWkQ0W1GEHIO4JBvbOnVZmmS4" But then I get the following errors: Traceback (most recent call last): File "C:\data\Projects\WildlifeObs_DataExportTool.py", line 72, in <module> fs.load(fsURL) File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\arcobjects\arcobjects.py", line 173, in load return convertArcObjectToPythonObject(self._arc_object.Load(*gp_fixargs(args))) RuntimeError: RecordSetObject: Cannot open table for Load There are a few things that are different about our configuration that may be the problem?: -it's a https address instead of http -there may be a problem with the web adapter because I have to use a port# in the tokenurl (tokenUrl = "https://FishAndWildlifeURL.gov:6443/arcgis/tokens/") -the machine I'm running the script on is behind a firewall that is different from where the feature service is stored Any ideas? Keep in mind that if I set the token to blank and log into the secured REST endpoint in my web browser I can run the code and get a feature class produced, so it still a problem with security and not the conversion of feat service to feat class.
... View more
11-06-2013
01:05 PM
|
0
|
0
|
1267
|
POST
|
I'd like to use the ESRI blog example for exporting a feature service to a feature class using Python. This simple example is found at the bottom of:http://blogs.esri.com/esri/arcgis/2013/10/10/quick-tips-consuming-feature-services-with-geoprocessing/ I've been able to get this to work for non-secured services or for a secured service where I am already logged in to the REST endpoint through my browser. However, I can't figure out the format for replacing the empty token variable used in the example with my actual ArcGIS Server credentials that I would use if I viewed the feature service in a browser. Thanks for any help you can provide.
... View more
11-05-2013
01:35 PM
|
0
|
4
|
5668
|
Title | Kudos | Posted |
---|---|---|
2 | 11-25-2013 11:40 AM |
Online Status |
Offline
|
Date Last Visited |
11-11-2020
02:24 AM
|