Learn how to implement user authentication User authentication is a type of authentication that allows users with an ArcGIS account to sign into an application and allow it to access ArcGIS content, services, and resources on their behalf. The typical authorization protocol used is OAuth2.0. Learn more to access a secure ArcGIS service A service, also known as an ArcGIS service, is software that supports an ArcGIS REST API and provides geospatial functionality or data. A service can be hosted by Esri or in ArcGIS Enterprise. Learn more with OAuth credentials OAuth credentials are an item that contains parameters required to implement user authentication or app authentication, including a client_id, client_secret, and redirect URIs. They are a type of developer credential. Learn more .

access services with oauth 2

You can use different types of authentication to access secured ArcGIS services A service, also known as an ArcGIS service, is software that supports an ArcGIS REST API and provides geospatial functionality or data. A service can be hosted by Esri or in ArcGIS Enterprise. Learn more . To implement OAuth credentials for user authentication User authentication is a type of authentication that allows users with an ArcGIS account to sign into an application and allow it to access ArcGIS content, services, and resources on their behalf. The typical authorization protocol used is OAuth2.0. Learn more , you can use your ArcGIS account An ArcGIS account is an identity with a user type and set of privileges that can access specific ArcGIS products, tools, APIs, services, and resources. The main account types that can be used for development are an ArcGIS Location Platform account, ArcGIS Online account, and ArcGIS Enterprise account. ArcGIS Location Platform and ArcGIS Online accounts are also associated with a subscription. Learn more to register an app with your portal ArcGIS portal, also known as a portal, is a website with applications and tools that can be used to create, manage, access, and share geospatial content and data. It supports security and authentication, developer credentials, content and data service management, user and group management, and site administration. A portal can be hosted in Esri's infrastructure or your own infrastructure. Learn more and get a Client ID A Client ID is an identifier associated with an application that assists with client / server OAuth 2.0 authentication for ArcGIS client APIs. Learn more , and then configure your app to redirect users to login with their credentials when the service or content is accessed. This is known as user authentication. If the app uses premium ArcGIS Online ArcGIS Online is a GIS mapping, analytics, data hosting, and content management software as a service (SaaS) product. It includes applications, tools, APIs, and location services for users and developers. It is subscription-based and requires an ArcGIS Online account. Learn more services that consume credits Credits are the currency used by ArcGIS Online Organization accounts to account for data storage and location service consumption. Credits are consumed for specific transactions, such as accessing location services, and types of storage, such as storing features, performing analytics, and using premium content. Learn more , for example, the app user’s account will be charged.

In this tutorial, you will build an app that implements user authentication using OAuth credentials so users can sign in and be authenticated through ArcGIS Online to access the ArcGIS World Traffic service.

Prerequisites

Before starting this tutorial:

  1. You need an ArcGIS Location Platform or ArcGIS Online account.

  2. Confirm that your system meets the minimum system requirements.

  3. An IDE for Java.

Steps

Create OAuth credentials for user authentication

OAuth credentials OAuth credentials are an item that contains parameters required to implement user authentication or app authentication, including a client_id, client_secret, and redirect URIs. They are a type of developer credential. Learn more are required to implement user authentication User authentication is a type of authentication that allows users with an ArcGIS account to sign into an application and allow it to access ArcGIS content, services, and resources on their behalf. The typical authorization protocol used is OAuth2.0. Learn more . These credentials are created and stored as an Application item An item, also known as a content item, is a resource stored in a portal such as a web map, hosted layer, style, script tool, file, or notebook. Learn more in your organization’s portal ArcGIS portal, also known as a portal, is a website with applications and tools that can be used to create, manage, access, and share geospatial content and data. It supports security and authentication, developer credentials, content and data service management, user and group management, and site administration. A portal can be hosted in Esri's infrastructure or your own infrastructure. Learn more .

  1. Go to the Create OAuth credentials for user authentication tutorial and create OAuth credentials OAuth credentials are an item that contains parameters required to implement user authentication or app authentication, including a client_id, client_secret, and redirect URIs. They are a type of developer credential. Learn more using your ArcGIS Location Platform An ArcGIS Location Platform account, formerly known as an ArcGIS Developer account, is an identity associated with an ArcGIS Location Platform subscription. Learn more or ArcGIS Online An ArcGIS Online account, also known as an ArcGIS Organization account, is an identity associated with an ArcGIS Online subscription. It can be used to access ArcGIS tools and develop applications with ArcGIS location services for an organization. Learn more account.

  2. Copy the Client ID and Redirect URL as you will use them to implement user authentication User authentication is a type of authentication that allows users with an ArcGIS account to sign into an application and allow it to access ArcGIS content, services, and resources on their behalf. The typical authorization protocol used is OAuth2.0. Learn more later in this tutorial. The Client ID is found on the Application item An item, also known as a content item, is a resource stored in a portal such as a web map, hosted layer, style, script tool, file, or notebook. Learn more ’s Overview page, while the Redirect URL is found on the Settings page.

Open a Java project with Gradle

  1. To start this tutorial, complete the Display a map tutorial, or download and unzip the Display a map solution into a new folder.

  2. Open the build.gradle file as a project in IntelliJ IDEA.

  3. Delete the code that sets your API Key. Since your app will be using OAuth, you will not need an API Key.

    App.java
    ArcGISRuntimeEnvironment.setApiKey("YOUR_ACCESS_TOKEN");

Prepare files before coding the app

Modify the files from the Display a map tutorial so they can be used in this tutorial: you will add imports, change the application title, and modify the view point’s scale.

  1. In IntelliJ IDEA, locate the Project tool window, open src/main/java/com.example.app, and double-click App. Add the following imports, replacing those from the Display a map tutorial.

    App.java
    package com.example.app;
    import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    import com.esri.arcgisruntime.layers.ArcGISMapImageLayer;
    import com.esri.arcgisruntime.mapping.ArcGISMap;
    import com.esri.arcgisruntime.mapping.BasemapStyle;
    import com.esri.arcgisruntime.mapping.Viewpoint;
    import com.esri.arcgisruntime.mapping.view.MapView;
    import com.esri.arcgisruntime.security.AuthenticationManager;
    import com.esri.arcgisruntime.security.DefaultAuthenticationChallengeHandler;
    import com.esri.arcgisruntime.security.OAuthConfiguration;
    public class App extends Application {
  2. In the start() life cycle method, change the title that will appear on the application window to Access services with OAuth 2.0. In addition, add the try statement, which will be closed later on in the tutorial.

    App.java
    @Override
    public void start(Stage stage) {
    try {
    // set title, size, and add scene to stage
    stage.setTitle("Access services with OAuth 2.0");
    stage.setWidth(800);
    stage.setHeight(700);
    stage.show();
  3. Change the scale of the map’s initial viewpoint to 72000. This scale will make the secured layer visible without zooming in.

    App.java
    58 collapsed lines
    /*
    * Copyright 2017 Esri.
    *
    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
    * use this file except in compliance with the License. You may obtain a copy of
    * the License at
    *
    * http://www.apache.org/licenses/LICENSE-2.0
    *
    * Unless required by applicable law or agreed to in writing, software
    * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    * License for the specific language governing permissions and limitations under
    * the License.
    */
    package com.example.app;
    import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    import com.esri.arcgisruntime.layers.ArcGISMapImageLayer;
    import com.esri.arcgisruntime.mapping.ArcGISMap;
    import com.esri.arcgisruntime.mapping.BasemapStyle;
    import com.esri.arcgisruntime.mapping.Viewpoint;
    import com.esri.arcgisruntime.mapping.view.MapView;
    import com.esri.arcgisruntime.security.AuthenticationManager;
    import com.esri.arcgisruntime.security.DefaultAuthenticationChallengeHandler;
    import com.esri.arcgisruntime.security.OAuthConfiguration;
    public class App extends Application {
    private MapView mapView;
    @Override
    public void start(Stage stage) {
    try {
    // set title, size, and add scene to stage
    stage.setTitle("Access services with OAuth 2.0");
    stage.setWidth(800);
    stage.setHeight(700);
    stage.show();
    // create stack pane and application scene
    StackPane stackPane = new StackPane();
    Scene scene = new Scene(stackPane);
    stage.setScene(scene);
    // create a map view and it to the stack pane
    mapView = new MapView();
    stackPane.getChildren().add(mapView);
    ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC);
    // set the map on the map view
    mapView.setMap(map);
    mapView.setViewpoint(new Viewpoint(34.02700, -118.80543, 72000.0 ));
    30 collapsed lines
    // set up the authentication manager to handle authentication challenges
    DefaultAuthenticationChallengeHandler defaultAuthenticationChallengeHandler = new DefaultAuthenticationChallengeHandler();
    AuthenticationManager.setAuthenticationChallengeHandler(defaultAuthenticationChallengeHandler);
    // add the OAuth configuration
    AuthenticationManager.addOAuthConfiguration(oAuthConfiguration);
    }
    /**
    * Stops and releases all resources used in application.
    */
    @Override
    public void stop() {
    if (mapView != null) {
    mapView.dispose();
    }
    }
    /**
    * Opens and runs application.
    *
    * @param args arguments passed to this application
    */
    public static void main(String[] args) {
    Application.launch(args);
    }
    }

Implement user authentication using OAuth 2.0

This API abstracts some of the details for user authentication User authentication is a type of authentication that allows users with an ArcGIS account to sign into an application and allow it to access ArcGIS content, services, and resources on their behalf. The typical authorization protocol used is OAuth2.0. Learn more using OAuth credentials OAuth credentials are an item that contains parameters required to implement user authentication or app authentication, including a client_id, client_secret, and redirect URIs. They are a type of developer credential. Learn more in your app. You can use classes such as AuthenticationManager to request, store, and manage credentials for secure resources.

Add code to set up the AuthenticationManager, which launches a small browser window titled Authentication Required. The user must enter log-in credentials before proceeding.

  1. In the start() method, after the line that calls mapView.setViewpoint(), create an OAuthConfiguration.

    App.java
    63 collapsed lines
    /*
    * Copyright 2017 Esri.
    *
    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
    * use this file except in compliance with the License. You may obtain a copy of
    * the License at
    *
    * http://www.apache.org/licenses/LICENSE-2.0
    *
    * Unless required by applicable law or agreed to in writing, software
    * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    * License for the specific language governing permissions and limitations under
    * the License.
    */
    package com.example.app;
    import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    import com.esri.arcgisruntime.layers.ArcGISMapImageLayer;
    import com.esri.arcgisruntime.mapping.ArcGISMap;
    import com.esri.arcgisruntime.mapping.BasemapStyle;
    import com.esri.arcgisruntime.mapping.Viewpoint;
    import com.esri.arcgisruntime.mapping.view.MapView;
    import com.esri.arcgisruntime.security.AuthenticationManager;
    import com.esri.arcgisruntime.security.DefaultAuthenticationChallengeHandler;
    import com.esri.arcgisruntime.security.OAuthConfiguration;
    public class App extends Application {
    private MapView mapView;
    @Override
    public void start(Stage stage) {
    try {
    // set title, size, and add scene to stage
    stage.setTitle("Access services with OAuth 2.0");
    stage.setWidth(800);
    stage.setHeight(700);
    stage.show();
    // create stack pane and application scene
    StackPane stackPane = new StackPane();
    Scene scene = new Scene(stackPane);
    stage.setScene(scene);
    // create a map view and it to the stack pane
    mapView = new MapView();
    stackPane.getChildren().add(mapView);
    ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC);
    // set the map on the map view
    mapView.setMap(map);
    mapView.setViewpoint(new Viewpoint(34.02700, -118.80543, 72000.0 ));
    // set up an OAuth config with URL to portal, a Client ID and a Redirect URL
    OAuthConfiguration oAuthConfiguration = new OAuthConfiguration("YOUR-ORGANIZATION-URL", "YOUR-APP-CLIENT-ID", "YOUR-APP-REDIRECT-URL");
    23 collapsed lines
    }
    /**
    * Stops and releases all resources used in application.
    */
    @Override
    public void stop() {
    if (mapView != null) {
    mapView.dispose();
    }
    }
    /**
    * Opens and runs application.
    *
    * @param args arguments passed to this application
    */
    public static void main(String[] args) {
    Application.launch(args);
    }
    }
  2. Create a DefaultAuthenticationChallengeHandler and set it on the AuthenticationManager. Then add the oAuthConfiguration to the AuthenticationManager.

    App.java
    63 collapsed lines
    /*
    * Copyright 2017 Esri.
    *
    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
    * use this file except in compliance with the License. You may obtain a copy of
    * the License at
    *
    * http://www.apache.org/licenses/LICENSE-2.0
    *
    * Unless required by applicable law or agreed to in writing, software
    * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    * License for the specific language governing permissions and limitations under
    * the License.
    */
    package com.example.app;
    import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    import com.esri.arcgisruntime.layers.ArcGISMapImageLayer;
    import com.esri.arcgisruntime.mapping.ArcGISMap;
    import com.esri.arcgisruntime.mapping.BasemapStyle;
    import com.esri.arcgisruntime.mapping.Viewpoint;
    import com.esri.arcgisruntime.mapping.view.MapView;
    import com.esri.arcgisruntime.security.AuthenticationManager;
    import com.esri.arcgisruntime.security.DefaultAuthenticationChallengeHandler;
    import com.esri.arcgisruntime.security.OAuthConfiguration;
    public class App extends Application {
    private MapView mapView;
    @Override
    public void start(Stage stage) {
    try {
    // set title, size, and add scene to stage
    stage.setTitle("Access services with OAuth 2.0");
    stage.setWidth(800);
    stage.setHeight(700);
    stage.show();
    // create stack pane and application scene
    StackPane stackPane = new StackPane();
    Scene scene = new Scene(stackPane);
    stage.setScene(scene);
    // create a map view and it to the stack pane
    mapView = new MapView();
    stackPane.getChildren().add(mapView);
    ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC);
    // set the map on the map view
    mapView.setMap(map);
    mapView.setViewpoint(new Viewpoint(34.02700, -118.80543, 72000.0 ));
    // set up an OAuth config with URL to portal, a Client ID and a Redirect URL
    OAuthConfiguration oAuthConfiguration = new OAuthConfiguration("YOUR-ORGANIZATION-URL", "YOUR-APP-CLIENT-ID", "YOUR-APP-REDIRECT-URL");
    // set up the authentication manager to handle authentication challenges
    DefaultAuthenticationChallengeHandler defaultAuthenticationChallengeHandler = new DefaultAuthenticationChallengeHandler();
    AuthenticationManager.setAuthenticationChallengeHandler(defaultAuthenticationChallengeHandler);
    // add the OAuth configuration
    AuthenticationManager.addOAuthConfiguration(oAuthConfiguration);
    24 collapsed lines
    }
    /**
    * Stops and releases all resources used in application.
    */
    @Override
    public void stop() {
    if (mapView != null) {
    mapView.dispose();
    }
    }
    /**
    * Opens and runs application.
    *
    * @param args arguments passed to this application
    */
    public static void main(String[] args) {
    Application.launch(args);
    }
    }
  3. Create a file named style.css to specify the visual display of the Authentication Required browser window.

    1. In the Project tool window, open src > main and right-click the main folder. In the context menu, click New > Directory and click resources under Gradle Source Sets. Hit Enter to create the folder.

    2. In the Project tool window, right-click the resources folder. In the context menu, click New > File and name the file style.css.

    3. Copy the code displayed below and paste it into the style.css file.

      style.css
      .panel-region .label {
      -fx-text-fill: white;
      }
      .label {
      -fx-text-fill: black;
      }
      .slider .axis {
      -fx-tick-label-fill: white;
      }
      .range-slider .axis {
      -fx-tick-label-fill: white;
      }
      .panel-region .check-box {
      -fx-text-fill: white;
      }
      .panel-region .radio-button {
      -fx-text-fill: white;
      }
      .color-picker .color-picker-label {
      -fx-text-fill: black;
      }

Add a traffic layer

You will add a layer to display the ArcGIS World Traffic service, a dynamic map service that presents historical and near real-time traffic information for different regions in the world. This is a secure service and requires an ArcGIS Online organizational subscription.

  1. Create an ArcGISMapImageLayer to display the traffic service. Then add the layer to the map’s collection of data layers (operational layers). In addition, you’ll add the catch statement that corresponds to the try statement added earlier in the tutorial.

    App.java
    65 collapsed lines
    /*
    * Copyright 2017 Esri.
    *
    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
    * use this file except in compliance with the License. You may obtain a copy of
    * the License at
    *
    * http://www.apache.org/licenses/LICENSE-2.0
    *
    * Unless required by applicable law or agreed to in writing, software
    * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    * License for the specific language governing permissions and limitations under
    * the License.
    */
    package com.example.app;
    import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    import com.esri.arcgisruntime.layers.ArcGISMapImageLayer;
    import com.esri.arcgisruntime.mapping.ArcGISMap;
    import com.esri.arcgisruntime.mapping.BasemapStyle;
    import com.esri.arcgisruntime.mapping.Viewpoint;
    import com.esri.arcgisruntime.mapping.view.MapView;
    import com.esri.arcgisruntime.security.AuthenticationManager;
    import com.esri.arcgisruntime.security.DefaultAuthenticationChallengeHandler;
    import com.esri.arcgisruntime.security.OAuthConfiguration;
    public class App extends Application {
    private MapView mapView;
    @Override
    public void start(Stage stage) {
    try {
    // set title, size, and add scene to stage
    stage.setTitle("Access services with OAuth 2.0");
    stage.setWidth(800);
    stage.setHeight(700);
    stage.show();
    // create stack pane and application scene
    StackPane stackPane = new StackPane();
    Scene scene = new Scene(stackPane);
    stage.setScene(scene);
    // create a map view and it to the stack pane
    mapView = new MapView();
    stackPane.getChildren().add(mapView);
    ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC);
    // set the map on the map view
    mapView.setMap(map);
    mapView.setViewpoint(new Viewpoint(34.02700, -118.80543, 72000.0 ));
    // set up an OAuth config with URL to portal, a Client ID and a Redirect URL
    OAuthConfiguration oAuthConfiguration = new OAuthConfiguration("YOUR-ORGANIZATION-URL", "YOUR-APP-CLIENT-ID", "YOUR-APP-REDIRECT-URL");
    // set up the authentication manager to handle authentication challenges
    DefaultAuthenticationChallengeHandler defaultAuthenticationChallengeHandler = new DefaultAuthenticationChallengeHandler();
    AuthenticationManager.setAuthenticationChallengeHandler(defaultAuthenticationChallengeHandler);
    // add the OAuth configuration
    AuthenticationManager.addOAuthConfiguration(oAuthConfiguration);
    ArcGISMapImageLayer trafficLayer = new ArcGISMapImageLayer("https://traffic.arcgis.com/arcgis/rest/services/World/Traffic/MapServer");
    map.getOperationalLayers().add(trafficLayer);
    } catch (Exception e) {
    // on any error, display the stack trace.
    e.printStackTrace();
    }
    }
    22 collapsed lines
    /**
    * Stops and releases all resources used in application.
    */
    @Override
    public void stop() {
    if (mapView != null) {
    mapView.dispose();
    }
    }
    /**
    * Opens and runs application.
    *
    * @param args arguments passed to this application
    */
    public static void main(String[] args) {
    Application.launch(args);
    }
    }
  2. Run the app. Ensure to run the app as a Gradle task and not as an application in your IDE. In the Gradle tool window, under Tasks > application, double-click run.

You should initially see the map A map is a collection of layers that are displayed in 2D. It is typically composed of a basemap layer and data layers. Learn more with the topographic basemap layer A basemap layer is the layer in a map or scene that displays basemap data. The data source for a basemap layer is typically a basemap service. Learn more centered on the Santa Monica Mountains in California. You will then be prompted for an ArcGIS Online username and password. After you authenticate successfully with ArcGIS Online, the traffic layer will appear in the map.

ArcGIS World Traffic service layer