Select to view content in your preferred language

Unable to Add Token for Secure Feature Services in Print Task

27
0
yesterday
Pa_Lotus
Regular Contributor

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.

 

To handle token-based authentication, I have configured the proxy and included the following in my HTML:

esri.config.defaults.io.proxyUrl = "../proxy.ashx";
esriConfig.defaults.io.alwaysUseProxy = true;

 

 

In my proxy.config file, I have the following entries:

 

<serverUrl url="https://myserver.com/gis/rest/services/Utilities/PrintingTools/GPServer/"
username="myUser" password="myPassword"
matchAll="true" />
<serverUrl url="https://myserver.com/gis/rest/services/Utilities/PrintingTools/GPServer/Export%20Web%20Map%20Task"
username="myUser" password="myPassword"
matchAll="true" />
<!-- Uncommented the below if needed -->
<!-- <serverUrl url="https://myserver.com/portal/sharing/rest/generateToken"
username="myUser" password="myPassword"
matchAll="true" /> -->
<serverUrl url="https://myserver.com/gis/rest/services/Utilities/Geometry/GeometryServer"
matchAll="true" />
<serverUrl url="https://myserver.com/gis/rest/services/MapsFeatureServer/"
username="myUser" password="myPassword"
matchAll="true" />

I’ve implemented the following logic to append a token to the FeatureServer URLs of the map layers before executing the print task:

 

var token = 'your_token_here';
var params = new PrintParameters();
params.map = this.map;

// Add token to each layer URL if it matches "FeatureServer"
Object.values(params.map._layers).forEach(layer => {
if (layer.url && layer.url.includes("FeatureServer")) {
// Append the token as a query parameter
if (!layer.url.includes("token=")) {
layer.url += (layer.url.includes("?") ? "&" : "?") + `token=${token}`;
console.log(`Token added to layer: ${layer.url}`);
}
}
});

// Now proceed with the print task
var printTask = new PrintTask(printURL);
var ptemplate = new PrintTemplate();
ptemplate.exportOptions = {
width: 500,
height: 500,
dpi: 96
};
ptemplate.layout = "MAP_ONLY";
params.template = ptemplate;

// Execute the print task
printTask.execute(params,
lang.hitch(this, this.PrintComplete),
lang.hitch(this, this.printError)
);

 

Issue:

Even though I’ve added the token to the FeatureServer layer URLs dynamically, the print task fails with the following error:

 

Error executing tool. Export Web Map Task: Failed to create layer from service at <FeatureServer_URL>. Failed to execute (Export Web Map).

 

What I’ve Tried:

  1. Adding the token dynamically to FeatureServer layer URLs before initiating the print task.
  2. Verifying that the token is valid.
  3. Ensuring that both the print service and map services are in the same group in the portal.

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?

Any guidance or recommendations would be greatly appreciated!

0 Kudos
0 Replies