How can I load a private layer giving the the App login data ?

1615
1
06-13-2016 10:28 PM
Amalka_Chandrasiri
New Contributor


Im trying to load a private feature layer using app login. I have my App ID and a generated token and I'm trying to feed that data to a credential object and load the feature layer. But when the layer get loaded it still askes for log in infomation even though I have them attacehd with the feature layer.

How can I load a feature layer providng my app login info so that it will not pop the login info dialog ? Please help

Find below the code I tired.

function layerLoad()

{

  require(["esri/request", "esri/layers/FeatureLayer", "esri/Credential"],

  function(esriRequest, FeatureLayer, Credential) {

     var layers = [];

     var featureLayer;

     var layerOptions;

     var token = "L7yIuZW-h9ODkLRHOpnWpYlxHzHVIUdUgKQQKHqPLOVrNHMYi3clvlCTF6V5Xw64Xor-TUsUtrmfKXnQ5iomxO6_ltRRGFpq-UlO01C4XnIN6wSxjtHp15ciXeUIsEEA3cknZKsFKA1IrE_P28zmug..";   

     var privateLayerUrl = "http://services.arcgis.com/u1pyDfVudZJk1pG6/arcgis/rest/services/Private_Layer/FeatureServer";

     var privateLayerWithToken = privateLayerUrl + "?token=" + token;

     var layersRequest = esriRequest({

       url: privateLayerWithToken,

       content: { f: "json" },

       //form:{ token: token },

       handleAs: "json",

       //callbackParamName: "callback"

   });

  var credentials = new Credential({

  userId:"eR4g2n1Qv1U18Od4", // App ID

  token: token

  });

  layersRequest.then(

  function(response) {

            layerOptions = {

    outFields: ["*"],

    mode: FeatureLayer.MODE_ONDEMAND,

    id: 55,

    //credential:credentials,

    visible: true

     };

            featureLayer = new FeatureLayer("http://services.arcgis.com/u1pyDfVudZJk1pG6/arcgis/rest/services/Private_Layer/FeatureServer/0", layerOptions);

  featureLayer.credential = credentials;

            layers.push(featureLayer);

  map.addLayers(response.layers);

  console.log("Success: ", response.layers);

     },

  function(error) {

   console.log("Error: ", error.message);

  }); 

  });

}

Tags (1)
0 Kudos
1 Reply
Amalka_Chandrasiri
New Contributor

Please note the credential object is commented in the above code example which is a mistake, When I try it was uncommented.

0 Kudos