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.

Before you begin

Reuse the starter project

If you have not done the starter project tutorial, be sure to review it first so you are familiar with running the app.

In a new or empty folder, make a copy of the Create a starter app solution.

Steps

Update the project

  1. Open the settings.gradle file in an editor of your choice and change the rootProject.name value to access-services-with-oauth-2.

Configure OAuth2 for your app

Register your app to generate a client id and set a redirect URI to the secure service:

  1. Sign in to your ArcGIS account. If you don't already have one, sign-up for free.

    1. Select drop-down menu Dashboard > New Application to create a new application.

      • Title: Access Private Layer App
      • Tags: OAuth
      • Description: Access Private Layer App
      • Click Register New Application
    2. At the top, click on the tab Authentication and under Redirect URLs add the following.

      • Redirect URL: urn:ietf:wg:oauth:2.0:oob
    3. Make note of the Client ID value as you'll need this in the next step.

Imports

  1. Open the App.java file in src/main/java/com/arcgis/developers/labs. Then merge the following with the existing imports.

    import java.net.MalformedURLException;
    import javafx.scene.control.Alert;
    import com.esri.arcgisruntime.layers.ArcGISMapImageLayer;
    import com.esri.arcgisruntime.loadable.LoadStatus;
    import com.esri.arcgisruntime.mapping.Viewpoint;
    import com.esri.arcgisruntime.portal.Portal;
    import com.esri.arcgisruntime.security.AuthenticationManager;
    import com.esri.arcgisruntime.security.DefaultAuthenticationChallengeHandler;
    import com.esri.arcgisruntime.security.OAuthConfiguration;
    

Configure OAuth in the app

  1. Create a new method to encapsulate authentication. Set up information to create an OAuthConfiguration which will prompt the user for credentials directly from arcgis.com.

    private void setupAuthentication() {
        String portalURL = "https://www.arcgis.com";
        String clientId = "YOUR_CLIENT_ID";
        String redirectURI = "urn:ietf:wg:oauth:2.0:oob";
    }
    
  2. Create an OAuthConfiguration and assign it to the AuthenticationManager, which will invoke this authentication when private access is needed.

         String redirectURI = "urn:ietf:wg:oauth:2.0:oob";
    
         /* ** ADD ** */
         try {
           OAuthConfiguration oAuthConfiguration = new OAuthConfiguration(portalURL, clientId, redirectURI);
           AuthenticationManager.setAuthenticationChallengeHandler(new DefaultAuthenticationChallengeHandler());
           AuthenticationManager.addOAuthConfiguration(oAuthConfiguration);
    
         } catch (MalformedURLException e) {
           e.printStackTrace();
         }
    
  3. Create a portal to access a private layer. Once the portal loads, call addTrafficLayer to load the traffic layer on the map.

     AuthenticationManager.addOAuthConfiguration(oAuthConfiguration);
    
     /* ** ADD ** */
     final Portal portal = new Portal(portalURL, true);
     portal.addDoneLoadingListener(() -> {
       if (portal.getLoadStatus() == LoadStatus.LOADED) {
     	  //addTrafficLayer(portal);
       } else {
         new Alert(Alert.AlertType.ERROR, "Portal: " + portal.getLoadError().getMessage()).show();
       }
     });
     portal.loadAsync();
    
  4. Update the start method to call setupAuthentication:

      setupMap();
    
      /* ** ADD ** */
      setupAuthentication();
    

Add a private layer to the map's operational layers

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

    private void addTrafficLayer(Portal portal) {
        String trafficURL = "https://traffic.arcgis.com/arcgis/rest/services/World/Traffic/MapServer";
        ArcGISMapImageLayer layer = new ArcGISMapImageLayer(trafficURL);
        layer.addDoneLoadingListener(() -> {
            if (portal.getLoadStatus() != LoadStatus.LOADED) {
                new Alert(Alert.AlertType.ERROR, "Layer: " + layer.getLoadError().getMessage()).show();
            }
        });
        mapView.getMap().getOperationalLayers().add(layer);
    }
    
  2. Update the setupAuthentication method to comment in addTrafficLayer(portal):

      if (portal.getLoadStatus() == LoadStatus.LOADED) {
          addTrafficLayer(portal);
      } else {
    

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!

Discover layers

Discover other layers you can use in your app. Explore ArcGIS Open Data and Los Angeles GeoHub for data you can use in your apps.