

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# 認証ヘルパーの使用方法
<a name="how-to-auth-helper"></a>

このセクションでは、認証ヘルパーに関する追加情報を説明します。

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

Amazon Location JavaScript 認証ユーティリティは、JavaScript アプリケーションから Amazon Location Service API コールを行う際の認証に役立ちます。これらのユーティリティは、API キーまたは Amazon Cognito を使用した認証に特に役立ちます。

**インストール**
+ NPM を使用してこのライブラリをインストールします。

  ```
  npm install @aws/amazon-location-utilities-auth-helper
  ```
+ ブラウザで直接使用するには、HTML ファイルに以下を含めます。

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

### 使用方法
<a name="loc-sdk-auth-usage"></a>

認証ヘルパーを使用するには、ライブラリをインポートし、必要なユーティリティ関数を呼び出します。このライブラリは、[Maps](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/geo-maps/)、[Places](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/geo-places/)、[Routes](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/geo-routes/) スタンドアロン SDK を含む Amazon Location Service SDK からのリクエストの認証と、[MapLibre GL JS](https://github.com/maplibre/maplibre-gl-js) を使用したマップのレンダリングをサポートしています。

**モジュールでの使用**

この例では、スタンドアロンの Places SDK を使用して 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);
```

この例では、スタンドアロンの Routes SDK を使用して 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);
```

この例では、API キー認証を使用した Location SDK を使用します。

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

**ブラウザでの使用**

ユーティリティ関数は、ブラウザ環境で直接使用する場合、amazonLocationAuthHelper グローバルオブジェクトでアクセスできます。

この例では、API キーで認証された Amazon Location Client を使用したリクエストを示します。

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

この例では、API キーで認証された MapLibre GL JS を使用してマップをレンダリングする方法を示します。

```
<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}`,
});
```

この例では、Amazon Cognito を使用して MapLibre GL JS でマップをレンダリングする方法を示します。

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

**認証された ID での代替利用方法**

withIdentityPoolId 関数を変更して、認証された ID のカスタムパラメータを含めることができます。

```
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 は、iOS アプリケーションから Amazon Location Service API へのリクエスト認証を支援します。特に API キーまたは Amazon Cognito による認証に役立ちます。

**インストール**
+ Xcode を開き、**[ファイル] > [パッケージの依存関係を追加]** に移動します。
+ 検索バーにパッケージ URL ([https://github.com/aws-geospatial/amazon-location-mobile-auth-sdk-ios/](https://github.com/aws-geospatial/amazon-location-mobile-auth-sdk-ios/)) を入力し、Enter キーを押します。
+ 「amazon-location-mobile-auth-sdk-ios」パッケージを選択し、**[パッケージを追加]** をクリックします。
+ 「AmazonLocationiOSAuthSDK」パッケージ製品を選択し、**[パッケージを追加]** をクリックします。

### 使用方法
<a name="loc-sdk-auth-usage"></a>

ライブラリをインストールしたら、`AuthHelper` クラスを使用して API キーまたは Amazon Cognito のクライアント設定を行います。

**API キー**

API キー認証でスタンドアロンの Places SDK を使用する例を次に示します。

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

API キー認証でスタンドアロンの Routes SDK を使用する例を次に示します。

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

API キー認証で Location SDK を使用する例を次に示します。

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

Amazon Cognito でスタンドアロンの Places SDK を使用する例を次に示します。

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

Amazon Cognito でスタンドアロン Routes SDK を使用する例を次に示します。

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

Amazon Cognito で Location SDK を使用する例を次に示します。

```
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 は、Android アプリケーションからの Amazon Location Service API へのリクエストを認証し、特に Amazon Cognito を使用した認証をサポートします。

**インストール**
+ この認証 SDK は、 AWS Kotlin SDK 全体と連携します。どちらの SDK も Maven Central に公開されます。[認証 SDK](https://mvnrepository.com/artifact/software.amazon.location/auth) の最新バージョンは、Maven Central で確認できます。
+ Android Studio の `build.gradle` ファイルの依存関係セクションに、次の行を追加します。

  ```
  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")
  ```
+ スタンドアロンの Maps、Places、Routes SDK の場合は、次の行を追加します。

  ```
  implementation("aws.sdk.kotlin:geomaps:1.3.65")
  implementation("aws.sdk.kotlin:geoplaces:1.3.65")
  implementation("aws.sdk.kotlin:georoutes:1.3.65")
  ```
+ Geofencing と Tracking を含む統合 Location SDK の場合は、次の行を追加します。

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

### 使用方法
<a name="loc-sdk-auth-usage"></a>

対象のコードで以下のクラスをインポートします。

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

を作成し`AuthHelper`、 AWS Kotlin SDK で使用できます。

**例: アイデンティティプール ID を持つ認証情報プロバイダー**

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

**例: カスタム認証情報プロバイダーを持つ認証情報プロバイダー**

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

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

`LocationCredentialsProvider` を使用して MapLibre マップをロードできます。以下がその例です。

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

作成したクライアントを使用して Amazon Location Service を呼び出します。以下の例では、指定した緯度と経度の近くにある場所を検索します。

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