This is starting to seem like an insurmountable task, so I am hoping I can get a few hints and pointers on how to do this. I need to automate the process of geocoding a db table and unfortunately our ArgGIS Enterprise server is still using ArcMap 10.4, so ArcPro is not an option. I have tried all of the obvious tricks like creating a model, running it to make sure it runs successfully, then exporting to a python script file. This doesn't work because the I need to use the esri World Geocode Service, and the url is different depending on whether you are geocoding from within ArcMap, or from a standalone script. I know the service url is "https://geocode.arcgis.com/arcgis/rest/services/ " and this seems to be consistent no matter the source doc (and the fact I was given this url by esri support).
Basically I have 2 issues:
1. finding the correct locator url (the second part of the url), for instance:
"World/GeocodeServer/ArcGIS World Geocoding Service"
"World.GeocodeServer"
"World/GeocodeServer"
2. Authenticating the request. So far I have tried creating a connection file, or signing in to ArcGIS online via arcpy.
Below are some examples of what I have tried. Of note, I spent a an hour yesterday doing a screenshare with esri support with no luck.
Here is the attempt with no connection file:
<SPAN class="keyword token">import</SPAN> arcpy
cwd <SPAN class="operator token">=</SPAN> sys<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
output_gdb_name <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Temp.gdb"</SPAN>
output_gdb <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>cwd<SPAN class="punctuation token">,</SPAN> output_gdb_name<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#Overwrite the output feature class if it already exists</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>overwriteOutput <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN>
<SPAN class="comment token">#creating field map for geocode service</SPAN>
field_input <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="string token">"Address"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"MailingAddressStreet"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"City"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"MailingAddressCityText"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"Postal"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"MailingAddressZipID"</SPAN>
<SPAN class="punctuation token">}</SPAN>
fieldMap <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>FieldInfo<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">for</SPAN> field <SPAN class="keyword token">in</SPAN> field_input<SPAN class="punctuation token">:</SPAN>
fieldMap<SPAN class="punctuation token">.</SPAN>addField<SPAN class="punctuation token">(</SPAN>field<SPAN class="punctuation token">,</SPAN> field_input<SPAN class="punctuation token">[</SPAN>field<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"VISIBLE"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"NONE"</SPAN><SPAN class="punctuation token">)</SPAN>
addr_table <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>output_gdb<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"WildlifeOperatorInformationTable"</SPAN><SPAN class="punctuation token">)</SPAN>
geocodeServer <SPAN class="operator token">=</SPAN> <SPAN class="string token">"https://geocode.arcgis.com/arcgis/rest/services"</SPAN>
<SPAN class="comment token">### I have tried each of these locators</SPAN>
addr_locator <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>geocodeServer<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"World.GeocodeServer"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#addr_locator = os.path.join(geocodeServer, "World/GeocodeServer")</SPAN>
<SPAN class="comment token">#addr_locator = os.path.join(geocodeServer, "World/ArcGIS World Geocoding Service")</SPAN>
geocode_result <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>output_gdb<SPAN class="punctuation token">,</SPAN><SPAN class="string token">"geocodeResult"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#geocoding addresses</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>GeocodeAddresses_geocoding<SPAN class="punctuation token">(</SPAN>addr_table<SPAN class="punctuation token">,</SPAN> addr_locator<SPAN class="punctuation token">,</SPAN> fieldMap<SPAN class="punctuation token">,</SPAN> geocode_result<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'STATIC'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>Attempted to create a connection file and get no error, but no connection file exists for output directory:
<SPAN class="keyword token">import</SPAN> arcpy<SPAN class="punctuation token">,</SPAN> os<SPAN class="punctuation token">,</SPAN> sys
cwd <SPAN class="operator token">=</SPAN> sys<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
output_gdb_name <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Temp.gdb"</SPAN>
output_gdb <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>cwd<SPAN class="punctuation token">,</SPAN> output_gdb_name<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#Overwrite the output feature class if it already exists</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>overwriteOutput <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN>
<SPAN class="comment token">#Create the ArcGIS Server connection file</SPAN>
outPath <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"...\NuisanceWildlifeTrappers"</SPAN>
<SPAN class="comment token">#outPath = "GIS Servers"</SPAN>
server_url <SPAN class="operator token">=</SPAN> <SPAN class="string token">"https://geocode.arcgis.com/arcgis/rest/services"</SPAN>
conn_file_name <SPAN class="operator token">=</SPAN> <SPAN class="string token">"arcGIS_geocoding.ags"</SPAN>
conn_file <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>outPath<SPAN class="punctuation token">,</SPAN> conn_file_name<SPAN class="punctuation token">)</SPAN>
user <SPAN class="operator token">=</SPAN> <SPAN class="string token">"John.Smith"</SPAN>
pwd <SPAN class="operator token">=</SPAN> <SPAN class="string token">"xyz"</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>CreateGISServerConnectionFile<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"USE_GIS_SERVICES"</SPAN><SPAN class="punctuation token">,</SPAN>
outPath<SPAN class="punctuation token">,</SPAN>
conn_file_name<SPAN class="punctuation token">,</SPAN>
server_url<SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"ARCGIS_SERVER"</SPAN><SPAN class="punctuation token">,</SPAN>
username<SPAN class="operator token">=</SPAN>user<SPAN class="punctuation token">,</SPAN>
password<SPAN class="operator token">=</SPAN>pwd<SPAN class="punctuation token">,</SPAN>
save_username_password<SPAN class="operator token">=</SPAN><SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">"completed"</SPAN>
<SPAN class="keyword token">print</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetMessages<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>I then created a GIS server connection using ArcCatalog for testing to see if I could get it to work.
locator <SPAN class="operator token">=</SPAN> <SPAN class="string token">"GIS Servers\arcgis on geocode.arcgis.com (user)"</SPAN>
field_input <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="string token">"Address"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"MailingAddressStreet"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"City"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"MailingAddressCityText"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"Postal"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"MailingAddressZipID"</SPAN>
<SPAN class="punctuation token">}</SPAN>
fieldMap <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>FieldInfo<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">for</SPAN> field <SPAN class="keyword token">in</SPAN> field_input<SPAN class="punctuation token">:</SPAN>
fieldMap<SPAN class="punctuation token">.</SPAN>addField<SPAN class="punctuation token">(</SPAN>field<SPAN class="punctuation token">,</SPAN> field_input<SPAN class="punctuation token">[</SPAN>field<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"VISIBLE"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"NONE"</SPAN><SPAN class="punctuation token">)</SPAN>
addr_table <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>output_gdb<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"WildlifeOperatorInformationTable"</SPAN><SPAN class="punctuation token">)</SPAN>
geocode_result <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>output_gdb<SPAN class="punctuation token">,</SPAN><SPAN class="string token">"geocodeResult_fromScript"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#geocoding addresses</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>GeocodeAddresses_geocoding<SPAN class="punctuation token">(</SPAN>addr_table<SPAN class="punctuation token">,</SPAN> locator<SPAN class="punctuation token">,</SPAN> fieldMap<SPAN class="punctuation token">,</SPAN> geocode_result<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'STATIC'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>....and got the following error:

I did try signing in to the portal by placing this code in the python script just before the geocode function but it makes no difference:
cwd <SPAN class="operator token">=</SPAN> sys<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
output_gdb_name <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Temp.gdb"</SPAN>
output_gdb <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>cwd<SPAN class="punctuation token">,</SPAN> output_gdb_name<SPAN class="punctuation token">)</SPAN>
field_input <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="string token">"Address"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"MailingAddressStreet"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"City"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"MailingAddressCityText"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"Postal"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"MailingAddressZipID"</SPAN>
<SPAN class="punctuation token">}</SPAN>
fieldMap <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>FieldInfo<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">for</SPAN> field <SPAN class="keyword token">in</SPAN> field_input<SPAN class="punctuation token">:</SPAN>
fieldMap<SPAN class="punctuation token">.</SPAN>addField<SPAN class="punctuation token">(</SPAN>field<SPAN class="punctuation token">,</SPAN> field_input<SPAN class="punctuation token">[</SPAN>field<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"VISIBLE"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"NONE"</SPAN><SPAN class="punctuation token">)</SPAN>
addr_table <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>output_gdb<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"WildlifeOperatorInformationTable"</SPAN><SPAN class="punctuation token">)</SPAN>
geocodeServer <SPAN class="operator token">=</SPAN> <SPAN class="string token">"https://geocode.arcgis.com/arcgis/rest/services"</SPAN>
addr_locator <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>geocodeServer<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"World.GeocodeServer"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#addr_locator = os.path.join(geocodeServer, "World/GeocodeServer")</SPAN>
<SPAN class="comment token">#addr_locator = os.path.join(geocodeServer, "World/ArcGIS World Geocoding Service")</SPAN>
geocode_result <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>output_gdb<SPAN class="punctuation token">,</SPAN><SPAN class="string token">"geocodeResult"</SPAN><SPAN class="punctuation token">)</SPAN>
portalUrl <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetActivePortalURL<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> portalUrl
arcpy<SPAN class="punctuation token">.</SPAN>SignInToPortal_server<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'John.Smith'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'xyz'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#geocoding addresses</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>GeocodeAddresses_geocoding<SPAN class="punctuation token">(</SPAN>addr_table<SPAN class="punctuation token">,</SPAN> addr_locator<SPAN class="punctuation token">,</SPAN> fieldMap<SPAN class="punctuation token">,</SPAN> geocode_result<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'STATIC'</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>SignOutFromPortal_server<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">"completed"</SPAN>
<SPAN class="keyword token">print</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetMessages<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>Got this error:

I also read the documentation about authenticating a request by generating a token and followed the steps by creating an application at developers.arcgis.com, but am not sure how generated the token and use it from a python script. There isn't any documentation I can find on how to do this.
Sorry for the long post, but I have been at this for a while. Thanks for letting me know what I am doing wrong and for any suggestions.