<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Unable to Add Token for Secure Feature Services in Print Task in ArcGIS JavaScript Maps SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/unable-to-add-token-for-secure-feature-services-in/m-p/1561824#M86129</link>
    <description>&lt;P&gt;I am working on an application that uses the ArcGIS JavaScript API 3.x for a map. I have a default print service and secure map services that require a token for access. Both the print service and map services are in the same group in the portal.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To handle token-based authentication, I have configured the &lt;STRONG&gt;proxy&lt;/STRONG&gt; and included the following in my HTML:&lt;/P&gt;&lt;P&gt;esri.config.defaults.io.proxyUrl = "../proxy.ashx";&lt;BR /&gt;esriConfig.defaults.io.alwaysUseProxy = true;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In my proxy.config file, I have the following entries:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;lt;serverUrl url="&lt;A href="https://myserver.com/gis/rest/services/Utilities/PrintingTools/GPServer/" target="_blank" rel="noopener"&gt;https://myserver.com/gis/rest/services/Utilities/PrintingTools/GPServer/&lt;/A&gt;"&lt;BR /&gt;username="myUser" password="myPassword"&lt;BR /&gt;matchAll="true" /&amp;gt;&lt;BR /&gt;&amp;lt;serverUrl url="&lt;A href="https://myserver.com/gis/rest/services/Utilities/PrintingTools/GPServer/Export%20Web%20Map%20Task" target="_blank" rel="noopener"&gt;https://myserver.com/gis/rest/services/Utilities/PrintingTools/GPServer/Export%20Web%20Map%20Task&lt;/A&gt;"&lt;BR /&gt;username="myUser" password="myPassword"&lt;BR /&gt;matchAll="true" /&amp;gt;&lt;BR /&gt;&amp;lt;!-- Uncommented the below if needed --&amp;gt;&lt;BR /&gt;&amp;lt;!-- &amp;lt;serverUrl url="&lt;A href="https://myserver.com/portal/sharing/rest/generateToken" target="_blank" rel="noopener"&gt;https://myserver.com/portal/sharing/rest/generateToken&lt;/A&gt;"&lt;BR /&gt;username="myUser" password="myPassword"&lt;BR /&gt;matchAll="true" /&amp;gt; --&amp;gt;&lt;BR /&gt;&amp;lt;serverUrl url="&lt;A href="https://myserver.com/gis/rest/services/Utilities/Geometry/GeometryServer" target="_blank" rel="noopener"&gt;https://myserver.com/gis/rest/services/Utilities/Geometry/GeometryServer&lt;/A&gt;"&lt;BR /&gt;matchAll="true" /&amp;gt;&lt;BR /&gt;&amp;lt;serverUrl url="&lt;A href="https://myserver.com/gis/rest/services/MapsFeatureServer/" target="_blank" rel="noopener"&gt;https://myserver.com/gis/rest/services/MapsFeatureServer/&lt;/A&gt;"&lt;BR /&gt;username="myUser" password="myPassword"&lt;BR /&gt;matchAll="true" /&amp;gt;&lt;/P&gt;&lt;P&gt;I’ve implemented the following logic to append a token to the FeatureServer URLs of the map layers before executing the print task:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;var token = 'your_token_here';&lt;BR /&gt;var params = new PrintParameters();&lt;BR /&gt;params.map = this.map;&lt;/P&gt;&lt;P&gt;// Add token to each layer URL if it matches "FeatureServer"&lt;BR /&gt;Object.values(params.map._layers).forEach(layer =&amp;gt; {&lt;BR /&gt;if (layer.url &amp;amp;&amp;amp; layer.url.includes("FeatureServer")) {&lt;BR /&gt;// Append the token as a query parameter&lt;BR /&gt;if (!layer.url.includes("token=")) {&lt;BR /&gt;layer.url += (layer.url.includes("?") ? "&amp;amp;" : "?") + `token=${token}`;&lt;BR /&gt;console.log(`Token added to layer: ${layer.url}`);&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;});&lt;/P&gt;&lt;P&gt;// Now proceed with the print task&lt;BR /&gt;var printTask = new PrintTask(printURL);&lt;BR /&gt;var ptemplate = new PrintTemplate();&lt;BR /&gt;ptemplate.exportOptions = {&lt;BR /&gt;width: 500,&lt;BR /&gt;height: 500,&lt;BR /&gt;dpi: 96&lt;BR /&gt;};&lt;BR /&gt;ptemplate.layout = "MAP_ONLY";&lt;BR /&gt;params.template = ptemplate;&lt;/P&gt;&lt;P&gt;// Execute the print task&lt;BR /&gt;printTask.execute(params,&lt;BR /&gt;lang.hitch(this, this.PrintComplete),&lt;BR /&gt;lang.hitch(this, this.printError)&lt;BR /&gt;);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;H4&gt;&lt;STRONG&gt;Issue:&lt;/STRONG&gt;&lt;/H4&gt;&lt;P&gt;Even though I’ve added the token to the FeatureServer layer URLs dynamically, the print task fails with the following error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Error executing tool. Export Web Map Task: Failed to create layer from service at &amp;lt;FeatureServer_URL&amp;gt;. Failed to execute (Export Web Map).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;H4&gt;&lt;STRONG&gt;What I’ve Tried:&lt;/STRONG&gt;&lt;/H4&gt;&lt;OL&gt;&lt;LI&gt;Adding the token dynamically to FeatureServer layer URLs before initiating the print task.&lt;/LI&gt;&lt;LI&gt;Verifying that the token is valid.&lt;/LI&gt;&lt;LI&gt;Ensuring that both the print service and map services are in the same group in the portal.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;Question: Is dynamically appending the token to the layer URLs before the print task execution the correct approach? If not, what is the recommended way to handle secure services in a print task?&lt;/P&gt;&lt;P&gt;Any guidance or recommendations would be greatly appreciated!&lt;/P&gt;</description>
    <pubDate>Fri, 22 Nov 2024 18:43:17 GMT</pubDate>
    <dc:creator>Pa_Lotus</dc:creator>
    <dc:date>2024-11-22T18:43:17Z</dc:date>
    <item>
      <title>Unable to Add Token for Secure Feature Services in Print Task</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/unable-to-add-token-for-secure-feature-services-in/m-p/1561824#M86129</link>
      <description>&lt;P&gt;I am working on an application that uses the ArcGIS JavaScript API 3.x for a map. I have a default print service and secure map services that require a token for access. Both the print service and map services are in the same group in the portal.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To handle token-based authentication, I have configured the &lt;STRONG&gt;proxy&lt;/STRONG&gt; and included the following in my HTML:&lt;/P&gt;&lt;P&gt;esri.config.defaults.io.proxyUrl = "../proxy.ashx";&lt;BR /&gt;esriConfig.defaults.io.alwaysUseProxy = true;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In my proxy.config file, I have the following entries:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;lt;serverUrl url="&lt;A href="https://myserver.com/gis/rest/services/Utilities/PrintingTools/GPServer/" target="_blank" rel="noopener"&gt;https://myserver.com/gis/rest/services/Utilities/PrintingTools/GPServer/&lt;/A&gt;"&lt;BR /&gt;username="myUser" password="myPassword"&lt;BR /&gt;matchAll="true" /&amp;gt;&lt;BR /&gt;&amp;lt;serverUrl url="&lt;A href="https://myserver.com/gis/rest/services/Utilities/PrintingTools/GPServer/Export%20Web%20Map%20Task" target="_blank" rel="noopener"&gt;https://myserver.com/gis/rest/services/Utilities/PrintingTools/GPServer/Export%20Web%20Map%20Task&lt;/A&gt;"&lt;BR /&gt;username="myUser" password="myPassword"&lt;BR /&gt;matchAll="true" /&amp;gt;&lt;BR /&gt;&amp;lt;!-- Uncommented the below if needed --&amp;gt;&lt;BR /&gt;&amp;lt;!-- &amp;lt;serverUrl url="&lt;A href="https://myserver.com/portal/sharing/rest/generateToken" target="_blank" rel="noopener"&gt;https://myserver.com/portal/sharing/rest/generateToken&lt;/A&gt;"&lt;BR /&gt;username="myUser" password="myPassword"&lt;BR /&gt;matchAll="true" /&amp;gt; --&amp;gt;&lt;BR /&gt;&amp;lt;serverUrl url="&lt;A href="https://myserver.com/gis/rest/services/Utilities/Geometry/GeometryServer" target="_blank" rel="noopener"&gt;https://myserver.com/gis/rest/services/Utilities/Geometry/GeometryServer&lt;/A&gt;"&lt;BR /&gt;matchAll="true" /&amp;gt;&lt;BR /&gt;&amp;lt;serverUrl url="&lt;A href="https://myserver.com/gis/rest/services/MapsFeatureServer/" target="_blank" rel="noopener"&gt;https://myserver.com/gis/rest/services/MapsFeatureServer/&lt;/A&gt;"&lt;BR /&gt;username="myUser" password="myPassword"&lt;BR /&gt;matchAll="true" /&amp;gt;&lt;/P&gt;&lt;P&gt;I’ve implemented the following logic to append a token to the FeatureServer URLs of the map layers before executing the print task:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;var token = 'your_token_here';&lt;BR /&gt;var params = new PrintParameters();&lt;BR /&gt;params.map = this.map;&lt;/P&gt;&lt;P&gt;// Add token to each layer URL if it matches "FeatureServer"&lt;BR /&gt;Object.values(params.map._layers).forEach(layer =&amp;gt; {&lt;BR /&gt;if (layer.url &amp;amp;&amp;amp; layer.url.includes("FeatureServer")) {&lt;BR /&gt;// Append the token as a query parameter&lt;BR /&gt;if (!layer.url.includes("token=")) {&lt;BR /&gt;layer.url += (layer.url.includes("?") ? "&amp;amp;" : "?") + `token=${token}`;&lt;BR /&gt;console.log(`Token added to layer: ${layer.url}`);&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;});&lt;/P&gt;&lt;P&gt;// Now proceed with the print task&lt;BR /&gt;var printTask = new PrintTask(printURL);&lt;BR /&gt;var ptemplate = new PrintTemplate();&lt;BR /&gt;ptemplate.exportOptions = {&lt;BR /&gt;width: 500,&lt;BR /&gt;height: 500,&lt;BR /&gt;dpi: 96&lt;BR /&gt;};&lt;BR /&gt;ptemplate.layout = "MAP_ONLY";&lt;BR /&gt;params.template = ptemplate;&lt;/P&gt;&lt;P&gt;// Execute the print task&lt;BR /&gt;printTask.execute(params,&lt;BR /&gt;lang.hitch(this, this.PrintComplete),&lt;BR /&gt;lang.hitch(this, this.printError)&lt;BR /&gt;);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;H4&gt;&lt;STRONG&gt;Issue:&lt;/STRONG&gt;&lt;/H4&gt;&lt;P&gt;Even though I’ve added the token to the FeatureServer layer URLs dynamically, the print task fails with the following error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Error executing tool. Export Web Map Task: Failed to create layer from service at &amp;lt;FeatureServer_URL&amp;gt;. Failed to execute (Export Web Map).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;H4&gt;&lt;STRONG&gt;What I’ve Tried:&lt;/STRONG&gt;&lt;/H4&gt;&lt;OL&gt;&lt;LI&gt;Adding the token dynamically to FeatureServer layer URLs before initiating the print task.&lt;/LI&gt;&lt;LI&gt;Verifying that the token is valid.&lt;/LI&gt;&lt;LI&gt;Ensuring that both the print service and map services are in the same group in the portal.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;Question: Is dynamically appending the token to the layer URLs before the print task execution the correct approach? If not, what is the recommended way to handle secure services in a print task?&lt;/P&gt;&lt;P&gt;Any guidance or recommendations would be greatly appreciated!&lt;/P&gt;</description>
      <pubDate>Fri, 22 Nov 2024 18:43:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/unable-to-add-token-for-secure-feature-services-in/m-p/1561824#M86129</guid>
      <dc:creator>Pa_Lotus</dc:creator>
      <dc:date>2024-11-22T18:43:17Z</dc:date>
    </item>
  </channel>
</rss>

