

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

# Cara menggunakan pembantu otentikasi
<a name="how-to-auth-helper"></a>

Bagian ini memberikan informasi tambahan tentang pembantu otentikasi.

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

Utilitas JavaScript otentikasi Lokasi Amazon membantu dalam mengautentikasi saat melakukan panggilan Amazon Location Service API dari JavaScript aplikasi. Utilitas ini secara khusus mendukung otentikasi menggunakan kunci API atau Amazon Cognito.

**Instalasi**
+ Instal pustaka ini menggunakan NPM:

  ```
  npm install @aws/amazon-location-utilities-auth-helper
  ```
+ Untuk menggunakannya langsung di browser, sertakan yang berikut ini dalam file HTML Anda:

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

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

Untuk menggunakan pembantu otentikasi, impor perpustakaan dan panggil fungsi utilitas yang diperlukan. [Pustaka ini mendukung autentikasi permintaan dari Amazon Location Service SDKs, termasuk [Maps](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/geo-maps/), [Places](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/geo-places/), dan [Routes](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/geo-routes/) mandiri SDKs, serta merender peta dengan MapLibre GL JS.](https://github.com/maplibre/maplibre-gl-js)

**Penggunaan dengan Modul**

Contoh ini menunjukkan penggunaan SDK Places mandiri untuk membuat permintaan yang diautentikasi dengan kunci 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);
```

Contoh ini menunjukkan penggunaan SDK Routes mandiri untuk membuat permintaan yang diautentikasi dengan kunci 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);
```

Contoh ini menggunakan Location SDK dengan autentikasi kunci 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);
```

**Penggunaan dengan Browser**

Fungsi utilitas dapat diakses di bawah objek global amazonLocationAuth Helper ketika digunakan langsung di lingkungan browser.

Contoh ini menunjukkan permintaan dengan Klien Lokasi Amazon, yang diautentikasi menggunakan kunci 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);
```

Contoh ini menunjukkan rendering peta dengan MapLibre GL JS, diautentikasi dengan kunci 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}`,
});
```

Contoh ini menunjukkan rendering peta dengan MapLibre GL JS menggunakan 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(),
});
```

**Penggunaan Alternatif dengan Identitas Terautentikasi**

Anda dapat memodifikasi fungsi withIdentityPool Id untuk menyertakan parameter kustom untuk identitas yang diautentikasi:

```
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>

Amazon Location Service Mobile Authentication SDK for iOS membantu mengautentikasi permintaan ke Amazon Location Service APIs dari aplikasi iOS. Ini secara khusus mendukung otentikasi melalui kunci API atau Amazon Cognito.

**Instalasi**
+ Buka Xcode dan pergi ke **File> Add Package Dependencies**.
+ Ketik URL paket ([https://github.com/aws-geospatial/amazon-location-mobile-auth-sdk-ios/](https://github.com/aws-geospatial/amazon-location-mobile-auth-sdk-ios/)) ke dalam bilah pencarian dan tekan Enter.
+ **Pilih paket "amazon-location-mobile-auth-sdk-ios” dan klik Add Package.**
+ Pilih produk paket “AmazonLocationiOSAuthSDK” dan klik **Add Package**.

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

Setelah menginstal pustaka, gunakan `AuthHelper` kelas untuk mengonfigurasi pengaturan klien untuk kunci API atau Amazon Cognito.

**Kunci API**

Berikut adalah contoh menggunakan Places SDK mandiri dengan autentikasi kunci 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)
}
```

Berikut adalah contoh menggunakan SDK Routes mandiri dengan autentikasi kunci 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)
}
```

Berikut adalah contoh menggunakan Location SDK dengan autentikasi kunci 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)
}
```

Berikut adalah contoh menggunakan SDK Places mandiri dengan 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)
}
```

Berikut adalah contoh menggunakan SDK Routes mandiri dengan 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)
}
```

Berikut adalah contoh menggunakan Location SDK dengan 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>

Amazon Location Service Mobile Authentication SDK for Android membantu Anda mengautentikasi permintaan ke Amazon Location APIs Service dari aplikasi Android, khususnya mendukung otentikasi menggunakan Amazon Cognito.

**Instalasi**
+ SDK otentikasi ini berfungsi dengan keseluruhan SDK AWS Kotlin. Keduanya SDKs dipublikasikan ke Maven Central. Periksa versi terbaru [SDK autentikasi](https://mvnrepository.com/artifact/software.amazon.location/auth) di Maven Central.
+ Tambahkan baris berikut ke bagian dependensi `build.gradle` file Anda di 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")
  ```
+ Untuk Peta, Tempat, dan Rute mandiri SDKs, tambahkan baris berikut:

  ```
  implementation("aws.sdk.kotlin:geomaps:1.3.65")
  implementation("aws.sdk.kotlin:geoplaces:1.3.65")
  implementation("aws.sdk.kotlin:georoutes:1.3.65")
  ```
+ Untuk SDK Lokasi konsolidasi yang mencakup Geofencing dan Pelacakan, tambahkan baris berikut:

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

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

Impor kelas berikut dalam kode Anda:

```
// 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
```

Anda dapat membuat `AuthHelper` dan menggunakannya dengan AWS Kotlin SDK:

**Contoh: Penyedia Kredensi dengan ID Kumpulan Identitas**

```
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())
}
```

**Contoh: Penyedia Kredensi dengan Penyedia Kredenal Kustom**

```
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())
}
```

**Contoh: Penyedia Kredensi dengan Kunci 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())
}
```

Anda dapat menggunakan `LocationCredentialsProvider` untuk memuat MapLibre peta. Inilah contohnya:

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

Gunakan klien yang dibuat untuk melakukan panggilan ke Amazon Location Service. Berikut adalah contoh yang mencari tempat di dekat garis lintang dan bujur tertentu:

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