1

I am looking to fetch some ArcGIS FeatureLayers from arcgis.com which are secured, using the JS 4.0 API. If I include them as normal, I get the pop-up window to allow users to log in. Ideally, this needs to use application authentication so end-users do not need to log in each time.

I have the App ID and App Secret details and can generate a token. If I append the token to the URL when making the new FeatureLayer, the first call is successful and I can see the token is sent through. But when that first call is completed, a second call is made to the /query method - this call no longer includes the token, which then fails the authentication and brings up the login window.

I am currently doing:

var layer = new FeatureLayer({
   url: "featureserverurl?token=token"
});

I have tried to pass the token through as a second option, though this is then never picked up on any of the requests. Is there something that I'm missing?

1

I think you'll need to register your token with the esri identity manager.

https://developers.arcgis.com/javascript/latest/api-reference/esri-identity-IdentityManager.html

There is a lot of black box functionality within the api when dealing with tokens and by registering the token with the id manager, you'll ensure that the rest of the api is "aware" of the token.

I've ended up using the initialize function of the identity manager before anything loads to add my token to the identity manager.

essentially, this looks like:

define(['esri/identity/Identitymanager'], function(id){
    id.initialize({
        'serverInfos': [{
            'server': path + '/arcgis',
            'tokenServiceUrl': path + '/arcgis/tokens/',
            'adminTokenServiceUrl': path + '/arcgis/admin/generateToken',
            'shortLivedTokenValidity': 480,
            'currentVersion': 10.41,
            'hasServer': true
        }],
        'oAuthInfos': [],
        'credentials': [{
            'userId': 'arcgis_user',
            'server': path + '/arcgis',
            'token': '<token_string>',
            'expires': <token_expiration>,
            'validity': 480,
            'ssl': true,
            'creationTime': new Date().getTime(),
            'scope': 'server',
            'resources': [
                path + '/arcgis/rest/services/internal/assets/MapServer',
                path + '/arcgis/rest/services/Utilities/PrintingTools/GPServer/Export%20Web%20Map%20Task'
            ]
        }]
    }
});

This json can actually be accessed if you set up the app to not have any tokens at first. Then a login dialog will be displayed, and the user can log in. Once a user is authenticated, open the browser console and type

esri.id.toJson()

That will print out the json for the initialize function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.