15 minutes

Requirements

Solution

Overview

You will learn: how to use named user login credentials to access the ArcGIS Traffic Layer service.

The ArcGIS Platform supports several security methodologies that you can implement to access ArcGIS premium services and secure content. To implement OAuth 2.0, you can use your ArcGIS account to register an application and get a client id, and then configure your app to redirect users to login with their credentials when the service or content is accessed. This is known as the "named user login" authentication pattern. If the app uses premium services that consume credits, the user's account will be charged, not your account. Alternatively, you can use your own account to set up service proxies that allow your applications to access premium services without asking the user to authenticate themselves. In this case, your account is providing authentication and will be charged credits if credit-consuming services are used. This is known as the "app login" authentication pattern. To learn more about service proxies, see the Set up authenticated services tutorial.

In this tutorial you will implement OAuth 2.0 to redirect users to sign in to ArcGIS so they can access the ArcGIS Traffic Layer service.

Create a portal to access a private layer. Once the portal loads, call addTrafficLayer to load the traffic layer on the map.

The addTrafficLayer is commented out and will be defined later.

Once the portal is loaded asynchronously, authentication will be invoked. Always verify the portal loaded correctly before continuing. Wrong credentials or a network timeout when trying to login could stop the portal from loading correctly.

Add a private layer to the map's operational layers

Create a new method to setup and add the traffic layer to the map as an operational layer.

The new method is named addTrafficLayer and requires a Portal object as a parameter. You call this method after authentication completes.

The traffic layer is loaded as an ArcGISMapImageLayer, a class derived from Layer, using the URL property set to the ArcGIS World Traffic Service.

The traffic layer is loaded over the network and therefore is not immediately available. Its asynchronous loading will trigger the AuthenticationManager and load once a valid token is provided to the service.

Congratulations, you're done!

When your application opens it will show a dialog from ArcGIS Online asking for OAuth login. Once successfully logged in you return to the application with the traffic layer added.

Challenge

Explore basemaps

Try different basemaps available with BasemapType. Now that you are able to authenticate users, you could also define private basemaps on your ArcGIS Online account and load those in your apps. Try it!