Select to view content in your preferred language

Arcgis enterprise authentication Using token

470
1
02-14-2024 08:44 PM
AsyrafArd
New Contributor

Hi everyone, I try to use token generate from arcgis server which are hosted locally in our server (arcgis Enterprise).

Im beginner in arcgis and I try to read sample from the documentation, however mostly are for arcgis online. 

package com.example.signup

import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.arcgismaps.ApiKey
import com.arcgismaps.ArcGISEnvironment
import com.arcgismaps.mapping.ArcGISMap
import com.arcgismaps.mapping.BasemapStyle
import com.arcgismaps.mapping.layers.ArcGISMapImageLayer
import com.arcgismaps.mapping.view.MapView
import com.example.signup.databinding.ActivityProfileBinding
class Profile : AppCompatActivity() {

private val activityProfileBinding: ActivityProfileBinding by lazy {
DataBindingUtil.setContentView(this, R.layout.activity_profile)
}

private val mapView: MapView by lazy {
activityProfileBinding.mapView
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

lifecycle.addObserver(mapView)

setApiKey()

setupMap()


}


private fun setupMap() {

val map = ArcGISMap(BasemapStyle.ArcGISImagery)
mapView.map = map

// Create an ArcGIS Map Image Layer for the ESRI map server layer
val mapServiceUrl = "https://10.1.156.146:6443/arcgis/rest/services/Jalan_Raya/MapServer"
val mapImageLayer = ArcGISMapImageLayer(mapServiceUrl)

map.operationalLayers.add(mapImageLayer)

}

private fun setApiKey() {

// It is not best practice to store API keys in source code. We have you insert one here
// to streamline this tutorial.

ArcGISEnvironment.apiKey = ApiKey.create("AAPK88529847185949ea825e3574ecc6c9b1qRO6NownHM5HmeA7l4dVMvm180y_oJcgTjabZ5qq3gdntEyRiFsBdzCVm7WItErn")
}

private fun showError(message: String) {
Toast.makeText(applicationContext, message, Toast.LENGTH_LONG).show()
Log.e(localClassName, message)
}

}

This is the error that i got.
A network authentication error occurred when accessing (ip numbers). A credential may be required. For more information see https://developers.arcgis.com/kotlin/security-and-authentication/.
can someone explain this?

 

0 Kudos
1 Reply
PuneetPrakash
Esri Contributor

The error signifies that you are trying to access a resource that is secured and not public. You will need to provide username/password for the NetworkAuthenticationChallenge to access that service. 

You can provide username/password by providing the implementation for the NetworkAuthenticationChallengeHandler interface

class PasswordCredentialAuthenticator(private val passwordCredential: PasswordCredential) :

NetworkAuthenticationChallengeHandler { 

override suspend fun handleNetworkAuthenticationChallenge(challenge: NetworkAuthenticationChallenge): NetworkAuthenticationChallengeResponse {
    return when (challenge.networkAuthenticationType) {
          NetworkAuthenticationType.UsernamePassword -> {
               NetworkAuthenticationChallengeResponse.ContinueWithCredential(passwordCredential)
               }
            else -> NetworkAuthenticationChallengeResponse.Cancel
          }
      }
}

and in your code you will set an instance of it on the AuthenticationManager, 

val passwordCredential = PasswordCredential("username", "password")
ArcGISEnvironment.authenticationManager.networkAuthenticationChallengeHandler =
PasswordCredentialAuthenticator(passwordCredential)
 
0 Kudos