Hey all,
I am attempting to use a Python script in ArcGIS Pro to overwrite a feature layer in AGOL. I want to use this script so that updates will be uploaded to AGOL nightly. I'm a newb when it comes to programming. I got this script from Kevin Himba (https://www.esri.com/arcgis-blog/products/api-python/analytics/updating-your-hosted-feature-services...)
I am currently getting an error code:
"ERROR 001272: Analyzer errors were encountered ([{"code":"00102","message":"Map does not contain a required layer type for web feature layer","object":"Recreation Asset Inventory"}]).
Failed to execute (StageService)."
One strange thing about the error is that the "Recreation Asset Inventory" is not even part of the script or the project I'm working in. It's a completely different feature layer.
Any ideas on how to proceed?
Thanks !
Josh
Perhaps you could provide the script in its entirety. It seems that the script is calling an aprx, and my guess is that aprx has the "Recreation Asset Inventory" feature layer in it.
Hey Jake,
Thanks for the recommendation. I am configuring this and trying to run the script.
I am now getting an error as "UnboundLocalError: local variable 'code' referenced before assignment."
The error references: AppData\Local\ESRI\conda\envs\arcgispro-py3-clone\lib\site-packages\arcgis\gis\_impl\_con\_connection.py", line 1276, in _oauth_token
redirect_uri="urn:ietf:wg:oauth:2.0:oob"
Partial Code for _connection.py:
if soup.title is not None:
if 'SUCCESS' in soup.title.string:
code = soup.title.string[len('SUCCESS code='):]
oauth = OAuth2Session(self._client_id,
redirect_uri="urn:ietf:wg:oauth:2.0:oob")
if code is None:
raise Exception("Could not generate a token.")
self._create_time = datetime.datetime.now()
token_info = oauth.fetch_token(tu, code=code, verify=False, include_client_id=True,
authorization_response='authorization_code')
self._refresh_token = token_info['refresh_token']
self._token = token_info['access_token']
self._expiration = token_info['expires_in']/60 - 2
return self._token
return None
Tried to make the 'code' variable a global variable as noted here (https://docs.python.org/3/faq/programming.html#why-am-i-getting-an-unboundlocalerror-when-the-variab...) but it didn't work.
Any suggestions?
@JoshBillingsmake sure your username is correct. It is case sensitive. Also, can you X out your password and send a screen shot of how you have the Variables section configured?
Variables screenshot below:
I had to add a client_id to get around sign-in errors.
@JoshBillingsit looks like you are trying to authenticate with an Enterprise account, which won't work for this script. Looks like you have a client ID. If you have a client Secret as well, you could create a token. Ex:
from arcgis import GIS
import requests, json
# Variables
username = 'joshuabi@CITYOFWS.ORG_CoWS' # AGOL username
clientId = "PAtQ6cf8UNmLthcm" # AGOL App ID
clientSecret = "********" # AGOL App Secret
fc = r"C:\Projects\Data.gdb\CustomerData" # Path to Feature Class
fsItemId = "c0e41f90407043a2b4ae6b2761c496db" # Feature Service Item ID to update
# Generate Token
params = {
'client_id': clientId,
'client_secret': clientSecret,
'grant_type': "client_credentials",
}
request = requests.get('https://www.arcgis.com/sharing/oauth2/token', params=params)
response = request.json()
token = response["access_token"]
# Create GIS object
gis = GIS('https://www.arcgis.com', token=token, verify_cert=False)
@JakeSkinner , I have a similar script I'm building for overwriting a feature layer. I like the suggestion you have shared, but I wonder if this could be simplified much further by using the overwrite method shown here:
https://developers.arcgis.com/python/sample-notebooks/overwriting-feature-layers/
I also wonder if it's possible to use a layer from a map in ArcGIS Pro as the input for the overwrite.
Any suggestions going this route would be helpful.
@JoeMagnottaI recommend using the following script rather than performing an overwrite of the feature service:
Thank you @JakeSkinner . I ended up using arcpy, looping through a list of layers in a map to complete the overwrite.
import arcpy
wrkspc = 'C:\\somePath'
myMap = 'someMap'
aprx = arcpy.mp.ArcGISProject(r"someProject.aprx")
m = aprx.listMaps(myMap)[0]
for lyr in m.listLayers():
sddraft = wrkspc + lyr.name + '.sddraft'
sd = wrkspc + lyr.name + '.sd'
arcpy.mp.CreateWebLayerSDDraft(lyr, sddraft, lyr.name,
'MY_HOSTED_SERVICES', 'FEATURE_ACCESS',
overwrite_existing_service=True)
arcpy.StageService_server(sddraft, sd)
arcpy.UploadServiceDefinition_server(sd, 'My Hosted Services')
This will publish/overwrite a separate feature service for every layer in a map. I like it because of its brevity and the ability to promote symbology changes and queries from the map.
I'll give the truncate method a shot. It seems compelling. Thanks!