Is there any way to call the arcpy.mp.CreateWebLayerSDDraft function outside of an active ArcGIS Pro session? I can't seem to find any way of programmatically logging in to ArcGIS Online through arcpy.
<SPAN class="string token">"""Publishes an ArcGIS Pro project's map to ArcGIS Online.
"""</SPAN>
<SPAN class="keyword token">import</SPAN> re
<SPAN class="keyword token">from</SPAN> os<SPAN class="punctuation token">.</SPAN>path <SPAN class="keyword token">import</SPAN> abspath<SPAN class="punctuation token">,</SPAN> exists
<SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="keyword token">from</SPAN> arcpy <SPAN class="keyword token">import</SPAN> AddMessage<SPAN class="punctuation token">,</SPAN> AddError
<SPAN class="keyword token">from</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mp <SPAN class="keyword token">import</SPAN> ArcGISProject<SPAN class="punctuation token">,</SPAN> CreateWebLayerSDDraft
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">main</SPAN><SPAN class="punctuation token">(</SPAN>project_path<SPAN class="operator token">=</SPAN><SPAN class="string token">'traffic-map.aprx'</SPAN><SPAN class="punctuation token">,</SPAN>
service_name<SPAN class="operator token">=</SPAN><SPAN class="string token">"Traveler_Info"</SPAN><SPAN class="punctuation token">,</SPAN>
folder_name<SPAN class="operator token">=</SPAN><SPAN class="string token">"Traveler_Info"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="string token">"""Publishes a project map to a service
"""</SPAN>
project_path <SPAN class="operator token">=</SPAN> abspath<SPAN class="punctuation token">(</SPAN>project_path<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="operator token">not</SPAN> exists<SPAN class="punctuation token">(</SPAN>project_path<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">raise</SPAN> FileNotFoundError<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"File not found: %s"</SPAN> <SPAN class="operator token">%</SPAN> project_path<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Open the project</SPAN>
AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Opening %s"</SPAN> <SPAN class="operator token">%</SPAN> project_path<SPAN class="punctuation token">)</SPAN>
aprx <SPAN class="operator token">=</SPAN> ArcGISProject<SPAN class="punctuation token">(</SPAN>project_path<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Get the first map</SPAN>
the_map <SPAN class="operator token">=</SPAN> aprx<SPAN class="punctuation token">.</SPAN>listMaps<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
the_layers <SPAN class="operator token">=</SPAN> the_map<SPAN class="punctuation token">.</SPAN>listLayers<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Create the output path string by replacing the file extension.</SPAN>
draft_path <SPAN class="operator token">=</SPAN> re<SPAN class="punctuation token">.</SPAN>sub<SPAN class="punctuation token">(</SPAN>r<SPAN class="string token">"\.aprx$"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">".sddraft"</SPAN><SPAN class="punctuation token">,</SPAN> project_path<SPAN class="punctuation token">)</SPAN>
AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Creating %s from %s..."</SPAN> <SPAN class="operator token">%</SPAN> <SPAN class="punctuation token">(</SPAN>project_path<SPAN class="punctuation token">,</SPAN> draft_path<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Create the web layer SDDraft file.</SPAN>
<SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># TODO: Fails here with a RuntimeError that has no message if</SPAN>
<SPAN class="comment token"># ArcGIS Pro is not open and signed in to ArcGIS Online.</SPAN>
CreateWebLayerSDDraft<SPAN class="punctuation token">(</SPAN>
the_layers<SPAN class="punctuation token">,</SPAN> draft_path<SPAN class="punctuation token">,</SPAN> service_name<SPAN class="punctuation token">,</SPAN> folder_name<SPAN class="operator token">=</SPAN>folder_name<SPAN class="punctuation token">,</SPAN>
copy_data_to_server<SPAN class="operator token">=</SPAN><SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">,</SPAN> summary<SPAN class="operator token">=</SPAN><SPAN class="string token">"Test service"</SPAN><SPAN class="punctuation token">,</SPAN>
tags<SPAN class="operator token">=</SPAN><SPAN class="string token">"test,traffic,traveler"</SPAN><SPAN class="punctuation token">,</SPAN> description<SPAN class="operator token">=</SPAN><SPAN class="string token">"Test Service"</SPAN><SPAN class="punctuation token">,</SPAN>
use_limitations<SPAN class="operator token">=</SPAN><SPAN class="string token">"For testing only"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">except</SPAN> RuntimeError <SPAN class="keyword token">as</SPAN> ex<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> len<SPAN class="punctuation token">(</SPAN>ex<SPAN class="punctuation token">.</SPAN>args<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">></SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">:</SPAN>
AddError<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Error creating %s. %s"</SPAN> <SPAN class="operator token">%</SPAN> <SPAN class="punctuation token">(</SPAN>draft_path<SPAN class="punctuation token">,</SPAN> ex<SPAN class="punctuation token">.</SPAN>args<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN>
AddError<SPAN class="punctuation token">(</SPAN>
<SPAN class="string token">"Error creating %s. No further info provided."</SPAN> <SPAN class="operator token">%</SPAN> draft_path<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN>
service_definition <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>server<SPAN class="punctuation token">.</SPAN>StageService<SPAN class="punctuation token">(</SPAN>draft_path<SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>server<SPAN class="punctuation token">.</SPAN>UploadServiceDefinition<SPAN class="punctuation token">(</SPAN>service_definition<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> __name__ <SPAN class="operator token">==</SPAN> <SPAN class="string token">'__main__'</SPAN><SPAN class="punctuation token">:</SPAN>
main<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></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>