

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

# Come usare gli aiutanti di autenticazione
<a name="how-to-auth-helper"></a>

Questa sezione fornisce informazioni aggiuntive sugli helper di autenticazione.

## App
<a name="loc-sdk-auth-web"></a>

Le utilità di JavaScript autenticazione di Amazon Location aiutano nell'autenticazione quando si effettuano chiamate API Amazon Location Service dalle applicazioni. JavaScript Queste utilità supportano specificamente l'autenticazione tramite chiavi API o Amazon Cognito.

**Installazione**
+ Installa questa libreria usando NPM:

  ```
  npm install @aws/amazon-location-utilities-auth-helper
  ```
+ Per utilizzarla direttamente nel browser, includi quanto segue nel tuo file HTML:

  ```
  <script src="https://cdn.jsdelivr.net/npm/@aws/amazon-location-utilities-auth-helper@1"></script>
  ```

### Utilizzo
<a name="loc-sdk-auth-usage"></a>

Per utilizzare gli helper di autenticazione, importate la libreria e richiamate le funzioni di utilità necessarie. [Questa libreria supporta l'autenticazione delle richieste da Amazon Location Service SDKs, incluso lo standalone [Maps](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/geo-maps/), [Places](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/geo-places/) e [Routes](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/geo-routes/) SDKs, oltre al rendering di mappe con MapLibre GL JS.](https://github.com/maplibre/maplibre-gl-js)

**Utilizzo con i moduli**

Questo esempio dimostra l'uso dell'SDK autonomo di Places per effettuare una richiesta autenticata con chiavi API:

```
npm install @aws-sdk/client-geo-places

import { GeoPlacesClient, GeocodeCommand } from "@aws-sdk/client-geo-places";
import { withAPIKey } from "@aws/amazon-location-utilities-auth-helper";

const authHelper = withAPIKey("<API Key>", "<Region>");
const client = new GeoPlacesClient(authHelper.getClientConfig());

const input = { ... };
const command = new GeocodeCommand(input);
const response = await client.send(command);
```

Questo esempio dimostra l'uso dell'SDK Routes standalone per effettuare una richiesta autenticata con chiavi API:

```
npm install @aws-sdk/geo-routes-client

import { GeoRoutesClient, CalculateRoutesCommand } from "@aws-sdk/geo-routes-client";
import { withAPIKey } from "@aws/amazon-location-utilities-auth-helper";

const authHelper = withAPIKey("<API Key>", "<Region>");
const client = new GeoRoutesClient(authHelper.getClientConfig());

const input = { ... };
const command = new CalculateRoutesCommand(input);
const response = await client.send(command);
```

Questo esempio utilizza Location SDK con autenticazione tramite chiave API:

```
npm install @aws-sdk/client-location

import { LocationClient, ListGeofencesCommand } from "@aws-sdk/client-location";
import { withAPIKey } from "@aws/amazon-location-utilities-auth-helper";

const authHelper = withAPIKey("<API Key>", "<Region>");
const client = new LocationClient(authHelper.getClientConfig());

const input = { ... };
const command = new ListGeofencesCommand(input);
const response = await client.send(command);
```

**Utilizzo con Browser**

Le funzioni di utilità sono accessibili tramite l'oggetto globale amazonLocationAuth Helper se utilizzate direttamente in un ambiente browser.

Questo esempio dimostra una richiesta con Amazon Location Client, autenticata tramite chiavi API:

```
<script src="https://cdn.jsdelivr.net/npm/@aws/amazon-location-client@1"></script>

const authHelper = amazonLocationClient.withAPIKey("<API Key>", "<Region>");
const client = new amazonLocationClient.GeoRoutesClient(authHelper.getClientConfig());
const input = { ... };
const command = new amazonLocationClient.routes.CalculateRoutesCommand(input);
const response = await client.send(command);
```

Questo esempio dimostra il rendering di una mappa con MapLibre GL JS, autenticata con una chiave API:

```
<script src="https://cdn.jsdelivr.net/npm/maplibre-gl@5.x"></script>

const apiKey = "<API Key>";
const region = "<Region>";
const styleName = "Standard";

const map = new maplibregl.Map({
  container: "map",
  center: [-123.115898, 49.295868],
  zoom: 10,
  style: `https://maps.geo.${region}.amazonaws.com/v2/styles/${styleName}/descriptor?key=${apiKey}`,
});
```

Questo esempio dimostra il rendering di una mappa con MapLibre GL JS utilizzando Amazon Cognito:

```
<script src="https://cdn.jsdelivr.net/npm/maplibre-gl@5.x"></script>
<script src="https://cdn.jsdelivr.net/npm/@aws/amazon-location-utilities-auth-helper@1"></script>

const identityPoolId = "<Identity Pool ID>";
const authHelper = await amazonLocationAuthHelper.withIdentityPoolId(identityPoolId);

const map = new maplibregl.Map({
  container: "map",
  center: [-123.115898, 49.295868],
  zoom: 10,
  style: `https://maps.geo.${region}.amazonaws.com/v2/styles/${styleName}/descriptor`,
  ...authHelper.getMapAuthenticationOptions(),
});
```

**Utilizzo alternativo con identità autenticate**

È possibile modificare la funzione withIdentityPool Id per includere parametri personalizzati per le identità autenticate:

```
const userPoolId = "<User Pool ID>";

const authHelper = await amazonLocationAuthHelper.withIdentityPoolId(identityPoolId, {
  logins: {
    [`cognito-idp.${region}.amazonaws.com/${userPoolId}`]: "cognito-id-token"
  }
});
```

## iOS
<a name="loc-sdk-auth-ios"></a>

L'Amazon Location Service Mobile Authentication SDK per iOS aiuta ad autenticare le richieste ad Amazon Location Service APIs dalle applicazioni iOS. Supporta specificamente l'autenticazione tramite chiavi API o Amazon Cognito.

**Installazione**
+ Apri Xcode e vai su **File > Aggiungi dipendenze del pacchetto**.
+ Digita l'URL del pacchetto ([https://github.com/aws-geospatial/amazon-location-mobile-auth-sdk-ios/](https://github.com/aws-geospatial/amazon-location-mobile-auth-sdk-ios/)) nella barra di ricerca e premi Invio.
+ **Seleziona il pacchetto "amazon-location-mobile-auth-sdk-ios» e fai clic su Aggiungi pacchetto.**
+ Scegli il prodotto del pacchetto "AmazonLocationiOSAuthSDK» e fai clic su **Aggiungi pacchetto**.

### Utilizzo
<a name="loc-sdk-auth-usage"></a>

Dopo aver installato la libreria, usa la `AuthHelper` classe per configurare le impostazioni del client per le chiavi API o Amazon Cognito.

**Chiavi API**

Ecco un esempio di utilizzo dell'SDK Places standalone con autenticazione tramite chiave API:

```
import AmazonLocationiOSAuthSDK
import AWSGeoPlaces

func geoPlacesExample() {
    let apiKey = "<API key>"
    let region = "<Region>"

    let authHelper = try await AuthHelper.withApiKey(apiKey: apiKey, region: region)
    let client: GeoPlacesClient = GeoPlacesClient(config: authHelper.getGeoPlacesClientConfig())

    let input = AWSGeoPlaces.SearchTextInput(
        biasPosition: [-97.7457518, 30.268193],
        queryText: "tacos"
    )

    let output = try await client.searchText(input: input)
}
```

Ecco un esempio di utilizzo dell'SDK Routes standalone con autenticazione tramite chiave API:

```
import AmazonLocationiOSAuthSDK
import AWSGeoRoutes

func geoRoutesExample() {
    let apiKey = "<API key>"
    let region = "<Region>"

    let authHelper = try await AuthHelper.withApiKey(apiKey: apiKey, region: region)
    let client: GeoRoutesClient = GeoRoutesClient(config: authHelper.getGeoRoutesClientConfig())

    let input = AWSGeoRoutes.CalculateRoutesInput(
        destination: [-123.1651031, 49.2577281],
        origin: [-97.7457518, 30.268193]
    )

    let output = try await client.calculateRoutes(input: input)
}
```

Ecco un esempio di utilizzo del Location SDK con autenticazione tramite chiave API:

```
import AmazonLocationiOSAuthSDK
import AWSLocation

func locationExample() {
    let apiKey = "<API key>"
    let region = "<Region>"

    let authHelper = try await AuthHelper.withApiKey(apiKey: apiKey, region: region)
    let client: LocationClient = LocationClient(config: authHelper.getLocationClientConfig())

    let input = AWSLocation.ListGeofencesInput(
        collectionName: "<Collection name>"
    )

    let output = try await client.listGeofences(input: input)
}
```

Ecco un esempio di utilizzo dell'SDK Places standalone con Amazon Cognito:

```
import AmazonLocationiOSAuthSDK
import AWSGeoPlaces

func geoPlacesExample() {
    let identityPoolId = "<Identity Pool ID>"

    let authHelper = try await AuthHelper.withIdentityPoolId(identityPoolId: identityPoolId)
    let client: GeoPlacesClient = GeoPlacesClient(config: authHelper.getGeoPlacesClientConfig())

    let input = AWSGeoPlaces.SearchTextInput(
        biasPosition: [-97.7457518, 30.268193],
        queryText: "tacos"
    )

    let output = try await client.searchText(input: input)
}
```

Ecco un esempio di utilizzo dell'SDK Routes standalone con Amazon Cognito:

```
import AmazonLocationiOSAuthSDK
import AWSGeoRoutes

func geoRoutesExample() {
    let identityPoolId = "<Identity Pool ID>"

    let authHelper = try await AuthHelper.withIdentityPoolId(identityPoolId: identityPoolId)
    let client: GeoRoutesClient = GeoRoutesClient(config: authHelper.getGeoRoutesClientConfig())

    let input = AWSGeoRoutes.CalculateRoutesInput(
        destination: [-123.1651031, 49.2577281],
        origin: [-97.7457518, 30.268193]
    )

    let output = try await client.calculateRoutes(input: input)
}
```

Ecco un esempio di utilizzo del Location SDK con Amazon Cognito:

```
import AmazonLocationiOSAuthSDK
import AWSLocation

func locationExample() {
    let identityPoolId = "<Identity Pool ID>"

    let authHelper = try await AuthHelper.withIdentityPoolId(identityPoolId: identityPoolId)
    let client: LocationClient = LocationClient(config: authHelper.getLocationClientConfig())

    let input = AWSLocation.ListGeofencesInput(
        collectionName: "<Collection name>"
    )

    let output = try await client.listGeofences(input: input)
}
```

## Android
<a name="loc-sdk-auth-android"></a>

L'Amazon Location Service Mobile Authentication SDK per Android ti aiuta ad autenticare le richieste ad Amazon Location APIs Service da applicazioni Android, supportando in particolare l'autenticazione tramite Amazon Cognito.

**Installazione**
+ Questo SDK di autenticazione funziona con l'SDK AWS Kotlin complessivo. Entrambi SDKs sono pubblicati su Maven Central. Controlla l'ultima versione dell'[SDK di autenticazione](https://mvnrepository.com/artifact/software.amazon.location/auth) su Maven Central.
+ Aggiungi le seguenti righe alla sezione delle dipendenze del tuo `build.gradle` file in Android Studio:

  ```
  implementation("software.amazon.location:auth:1.1.0")
  implementation("org.maplibre.gl:android-sdk:11.5.2")
  implementation("com.squareup.okhttp3:okhttp:4.12.0")
  ```
+ Per la versione autonoma di Maps, Places e Routes SDKs, aggiungi le seguenti righe:

  ```
  implementation("aws.sdk.kotlin:geomaps:1.3.65")
  implementation("aws.sdk.kotlin:geoplaces:1.3.65")
  implementation("aws.sdk.kotlin:georoutes:1.3.65")
  ```
+ Per il Location SDK consolidato che include Geofencing and Tracking, aggiungi la seguente riga:

  ```
  implementation("aws.sdk.kotlin:location:1.3.65")
  ```

### Utilizzo
<a name="loc-sdk-auth-usage"></a>

Importa le seguenti classi nel tuo codice:

```
// For the standalone Maps, Places, and Routes SDKs
import aws.sdk.kotlin.services.geomaps.GeoMapsClient
import aws.sdk.kotlin.services.geoplaces.GeoPlacesClient
import aws.sdk.kotlin.services.georoutes.GeoRoutesClient

// For the consolidated Location SDK
import aws.sdk.kotlin.services.location.LocationClient

import software.amazon.location.auth.AuthHelper
import software.amazon.location.auth.LocationCredentialsProvider
import software.amazon.location.auth.AwsSignerInterceptor
import org.maplibre.android.module.http.HttpRequestUtil
import okhttp3.OkHttpClient
```

Puoi crearne uno `AuthHelper` e usarlo con AWS Kotlin SDK:

**Esempio: fornitore di credenziali con ID Identity Pool**

```
private suspend fun exampleCognitoLogin() {
    val authHelper = AuthHelper.withCognitoIdentityPool("MY-COGNITO-IDENTITY-POOL-ID", applicationContext)
    
    var geoMapsClient = GeoMapsClient(authHelper?.getGeoMapsClientConfig())
    var geoPlacesClient = GeoPlacesClient(authHelper?.getGeoPlacesClientConfig())
    var geoRoutesClient = GeoRoutesClient(authHelper?.getGeoRoutesClientConfig())
    
    var locationClient = LocationClient(authHelper?.getLocationClientConfig())
}
```

**Esempio: fornitore di credenziali con provider di credenziali personalizzato**

```
private suspend fun exampleCustomCredentialLogin() {
    var authHelper = AuthHelper.withCredentialsProvider(MY-CUSTOM-CREDENTIAL-PROVIDER, "MY-AWS-REGION", applicationContext)

    var geoMapsClient = GeoMapsClient(authHelper?.getGeoMapsClientConfig())
    var geoPlacesClient = GeoPlacesClient(authHelper?.getGeoPlacesClientConfig())
    var geoRoutesClient = GeoRoutesClient(authHelper?.getGeoRoutesClientConfig())
    
    var locationClient = LocationClient(authHelper?.getLocationClientConfig())
}
```

**Esempio: fornitore di credenziali con chiave API**

```
private suspend fun exampleApiKeyLogin() {
    var authHelper = AuthHelper.withApiKey("MY-API-KEY", "MY-AWS-REGION", applicationContext)

    var geoMapsClient = GeoMapsClient(authHelper?.getGeoMapsClientConfig())
    var geoPlacesClient = GeoPlacesClient(authHelper?.getGeoPlacesClientConfig())
    var geoRoutesClient = GeoRoutesClient(authHelper?.getGeoRoutesClientConfig())
    
    var locationClient = LocationClient(authHelper?.getLocationClientConfig())
}
```

È possibile utilizzare `LocationCredentialsProvider` per caricare la MapLibre mappa. Ecco un esempio:

```
HttpRequestUtil.setOkHttpClient(
    OkHttpClient.Builder()
        .addInterceptor(
            AwsSignerInterceptor(
                "geo",
                "MY-AWS-REGION",
                locationCredentialsProvider,
                applicationContext
            )
        )
        .build()
)
```

Usa i client creati per effettuare chiamate verso Amazon Location Service. Ecco un esempio di ricerca di luoghi vicini a una latitudine e una longitudine specificate:

```
val suggestRequest = SuggestRequest {
       biasPosition = listOf(-97.718833, 30.405423)
       maxResults = MAX_RESULT
       language = "PREFERRED-LANGUAGE"
   }
val nearbyPlaces = geoPlacesClient.suggest(suggestRequest)
```