ArcGIS Runtime SDK for Java

OAuthentication

Demonstrates how to use the OAuth 2.0 protocol for authentication into the ArcGIS platform. Once authenticated, the sample displays the user's profile information.

How it works

To authenticate into the ArcGIS platform:

  1. Setup an OAuthConfiguration with the settings of an application registered in the ArcGIS platform.
  2. Setup an AuthenticationChallengeHandler that challenges the user for authentication. You could create a custom handler similar to the one created in this sample: OAuthChallengeHandler.
  3. On trying to access a secured resource, the authentication challenge in invoked.
  4. OAuthChallengeHandler directs the user to a sign-in page (using a WebView) from ArcGIS platform.
  5. On successful sign-in, the ArcGIS platform provides an authorization code.
  6. Use the authorization code to create a OAuthTokenCredentialRequest. This will be used by the Runtime to request an access token. The access token is then used to request a secured resource in the ArcGIS platform.

Code

/*
 * 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.esri.samples.portal.oauth;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class OAuthSample extends Application {

  @Override
  public void start(Stage stage) throws Exception {
    FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/oauth_controller.fxml"));
    Parent root = loader.load();
    OAuthController controller = loader.getController();
    Scene scene = new Scene(root);

    // set title, size, and add scene to stage
    stage.setTitle("OAuth Sample");
    stage.setWidth(800);
    stage.setHeight(700);
    stage.setScene(scene);
    stage.show();

    controller.authenticate();
  }

  /**
   * Opens and runs application.
   *
   * @param args arguments passed to this application
   */
  public static void main(String[] args) {
    Application.launch(args);
  }

}


In this topic
  1. How it works
  2. Code