Hello,
Newb question on the Javascript SDK - first time playing around to see what I can do with it, and falling at the first hurdle...
I've followed this tutorial on how to add a feature layer to a map, but using a non-public layer that is hosted by my ArcGIS Online account (where the API key is also coming from).
Everything works, except for the feature layer, which doesn't display.
Checking the response in developer tools for the feature layer and I can see the following error:
{"error":{"code":403,"message":"You do not have permissions to access this resource or perform this operation.","messageCode":"GWM_0003","details":["You do not have permissions to access this resource or perform this operation."]}}
I'm a bit confused by this, as the feature layer is owned by the same account that the API key is tied to. So I assumed it wouldn't have any permissions issues or need for additional tokens. No doubt I'm just fundamentally misunderstanding how this should work!
Can anyone put me straight on this? How can I get my non-public AGOL feature layer into a JS SDK map?
Full code below:
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
<title>ArcGIS Maps SDK for JavaScript Tutorials: Display a map</title>
<style>
html,
body,
#viewDiv {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
}
</style>
<link rel="stylesheet" href="https://js.arcgis.com/4.27/esri/themes/light/main.css">
<script src="https://js.arcgis.com/4.27/"></script>
<script>
require(["esri/config", "esri/Map", "esri/views/MapView", "esri/layers/FeatureLayer"], function(esriConfig, Map, MapView,FeatureLayer) {
esriConfig.apiKey = redacted;
const map = new Map({
basemap: "arcgis-topographic" // Basemap layer service
});
const view = new MapView({
map: map,
center: [-1.7468262, 53.826597], // Longitude, latitude
zoom: 6, // Zoom level
container: "viewDiv" // Div element
});
// Layer
const cLayer = new FeatureLayer({
url: "https://services2.arcgis.com/redacted/FeatureServer/0"
});
map.add(cLayer,0);
});
</script>
</head>
<body>
<div id="viewDiv"></div>
</body>
</html>
Have you tried applying the API key to the layer itself?
https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-FeatureLayer.html#apiKey
Thanks for the suggestion - sadly that didn't work. Applying the API key at that level (in addition to or in replacement of the primary key) just seems to kill everything and only returns a blank screen 😞
Did you add the layer to your API key scope in the API Key management console?
Layers via API Keys is still a beta feature, but you can try it out.
Thanks for the suggestion - I can't seem to see that scope, but I'm wondering if that's because my developer account is an ArcGIS Online organisation administrator account, as opposed to a standalone developer account.
I also note when I follow that link it says' ArcGIS Online accounts cannot be used to access private content items in ArcGIS'. I don't 100% follow that, but perhaps that means I can't do what I'm trying to do without sharing the layer publicly? i.e. any other form of sharing is deemed 'private', including organisational content, which is what this is.
Organization accounts are different from developer accounts. Org accounts should not be using API keys. That's why you don't have that option. Sorry, it may not be completely clear in all the doc, but org accounts should be using OAuth.
Info: here: https://developers.arcgis.com/javascript/latest/secure-resources/
Thanks, yes that is coming clearer now!
Just one final question - I didn't cover the use-case - but effectively the idea was it would be really handy to have a basic application, displaying a single key non-public layer, company-wide - without warranting AGOL accounts for everyone.
If I understand OAuth correctly, this isn't possible is it? As it would place authentication at the account level - and accounts can't be shared, so there's no point continuing this! That's why the API method seemed like a nice solution, if it worked.