Accessing REST API Query with username and password

16057
10
Jump to solution
12-22-2017 08:37 AM
Zeke
by
Regular Contributor III

I have the following code that I want to use to access a secured arc rest api site and query a layer to return the data in json. I can do this manually by going to the rest site in a browser, signing in, and using the query page to return data. And the code below (mostly copied from the web) works on non secured sites, without username and password.

But when I try to access the site using the password, I get the error:

RuntimeError: RecordSetObject: Cannot open table for Load  

How can I do log in programmatically to get this data? Thanks.

    baseURL = "http://company/arcgis/rest/services/Maps/CityMapServer/6/query"
    where = "1=1"
    fields = "*"
    user = "user"
    password = "pass"
    query = "?where={}&outFields={}&returnGeometry=true&f=json&auth=({},{})".format(where, fields, user, password)
    fsURL = baseURL + query
    fs = arcpy.FeatureSet()
    fs.load(fsURL)

0 Kudos
10 Replies
JamesGraham
New Contributor III

Just wanted to add this for future generations who are still working with ArcGIS Server-based Feature Services and want to use arcpy/Python to authenticate via token to do stuff.

I could not get any of the above to work in my environments (10.8, 10.6.1), but found an outstanding Python toolset called 'restapi' that is great if you still need to run things on Python 2.7.x and more traditional ArcGIS Servers (that are not Portal).

  • Authenticating is easy (token)
  • Editing is easy (including adding/editing attachments for feature services)

Highly recommend this repository!

GitHub - Bolton-and-Menk-GIS/restapi: Python API designed to work externally with ArcGIS REST Servic...