Select to view content in your preferred language

Print WebMap ArcGIS Online

1367
3
Jump to solution
01-06-2022 08:59 AM
MarcHoogerwerf_TAP
Occasional Contributor

Hi I'm trying to print/export a WebMap from ArcGIS Online through the API for Python but getting an invalid token error. I have no issue interacting with the WebMap itself, so the token should be valid. I can display the WebMap in a Jupyter Notebook, update the WebMap in a standalone script, etc.

It's only when the print line, indicated in bold below gets executed, processing starts, takes a while and then returns with the error message invalid token.

Error message in Jupyter Notebook

---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
In  [81]:
Line 7:     url = wm.print(file_format="PNG32",extent=ext)

File C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\mapping\_types.py, in print:
Line 2165:  layout_template=layout_template,

File C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\mapping\_utils.py, in export_map:
Line 99:    gis, "Export Web Map Task", kwargs, param_db, return_values, _use_async, url

File C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\geoprocessing\_support.py, in _execute_gp_tool:
Line 513:   resp = gptool._con.post(exec_url, gp_params)

File C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\gis\_impl\_con\_connection.py, in post:
Line 1079:  force_bytes=kwargs.pop("force_bytes", False),

File C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\gis\_impl\_con\_connection.py, in _handle_response:
Line 625:   self._handle_json_error(data["error"], errorcode)

File C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\gis\_impl\_con\_connection.py, in _handle_json_error:
Line 648:   raise Exception(errormessage)

Exception: Invalid Token
(Error Code: 498)
---------------------------------------------------------------------------

Error in a standalone Python script:

Exception has occurred: Exception
Invalid Token (Error Code: 498)
During handling of the above exception, another exception occurred: File "D:\OneDrive - Trans Adriatic Pipeline AG\Scripts\Python\JourneyManagementPlan\calculate_route.py", line 139, in print_webmap url = wm.print(file_format="PNG32",extent=ext,layout_template="MAP_ONLY") File "D:\OneDrive - Trans Adriatic Pipeline AG\Scripts\Python\JourneyManagementPlan\calculate_route.py", line 167, in main print_webmap(ext) File "D:\OneDrive - Trans Adriatic Pipeline AG\Scripts\Python\JourneyManagementPlan\calculate_route.py", line 170, in <module> main()

 

Here is the relevant code snippet. 

def print_webmap(ext😞

    wm_item = gis.content.get(WM_ITEM_ID)
    wm = WebMap(wm_item)
    url = wm.print(file_format="PNG32",extent=ext,layout_template="MAP_ONLY")
    with requests.get(url) as resp:
        with open('./output_file.png', 'wb') as file_handle:
            file_handle.write(resp.content)
 
sr = SpatialReference(4326)
x_start = 24.38
y_start = 40.93
x_end = 24.30
y_end = 40.95
 
ext = {"spatialReference": sr,
           "xmin":x_start,
           "ymin":y_start,
           "xmax":x_end,
           "ymax":y_end
    }
print_webmap(ext)
 
 
Tags (3)
1 Solution

Accepted Solutions
MarcHoogerwerf_TAP
Occasional Contributor

Thanks

Turned out that the web map contained one layer that used stored credentials....

As the layer was not required I removed that layer before printing the webmap.

View solution in original post

0 Kudos
3 Replies
AdminGIS2
Occasional Contributor

If the webmap contains secured services you might have to supply a valid token to the layer definition as well in order for the print function to work. You could try something like this:

for lyr in wm.layers:
    lyr['layerDefinition']['token'] = gis._conn.token

 

0 Kudos
Aymen_Farhat
Emerging Contributor

Tried but still getting same error of invalid token.

0 Kudos
MarcHoogerwerf_TAP
Occasional Contributor

Thanks

Turned out that the web map contained one layer that used stored credentials....

As the layer was not required I removed that layer before printing the webmap.

0 Kudos