@aws/amazon-location-utilities-auth-helper

中文文档
1.3.3发布于 2026/05/09Apache-2.0TypeScriptESM + CJS构建来源已验证近 30 天下载 21.3万

Amazon 位置工具 - JavaScript 认证帮助器

中文使用文档
文档目录11 节
  1. 安装
  2. 使用量
  3. 模块的使用
  4. 使用浏览器
  5. 文档
  6. withAPIKey
  7. withIdentityPoolId
  8. withCredentialProvider
  9. 获取帮助
  10. 捐款
  11. 许可证

Amazon 位置工具 - JavaScript 认证帮助器

Version Tests

这些是公用设备 用来帮助客户验证 亚马逊定位服务 API从他们的JavaScript应用程序中调用. 这在使用时特别有帮助 API 密钥, (中文(简体) ), 亚马逊-科尼奥托,或自定义证书提供者作为认证方法.

安装

从国家防范机制安装此库供模块使用:

npm install @aws/amazon-location-utilities-auth-helper

导入 HTML 文件, 直接用于浏览器 .

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

使用量

导入库并按需要调用顶级命名空间中的公用功能 。 此库用于验证独立请求 地图, (中文(简体) ), 地点,以及 路线 SDKs,该 地点 SDK,在绘制地图时 MapLibre GL 联合来文。 。 。 .

您可以在 文档 节.

模块的使用

这些例子显示在模块中导入我们的库,然后使用捆绑器将您的模块组合成一个可以在浏览器或其他环境中运行的脚本.

此示例使用独立的 地点 SDK可以提出使用API密钥认证的请求.

npm install @aws-sdk/geo-places-client
// Import from the AWS JavaScript SDK V3 (GeoPlacesClient)
import { GeoPlacesClient, GeocodeCommand } from "@aws-sdk/geo-places-client";
// Import the utility functions
import { withAPIKey } from "@aws/amazon-location-utilities-auth-helper";

// Create an authentication helper instance using an API key and region
const authHelper = withAPIKey("<API Key>", "<Region>");

// Configures the client to use API keys when making supported requests
const client = new GeoPlacesClient(authHelper.getClientConfig());

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

此示例使用独立的 路线 SDK可以提出使用API密钥认证的请求.

npm install @aws-sdk/geo-routes-client
// Import from the AWS JavaScript SDK V3 (GeoRoutesClient)
import { GeoRoutesClient, CalculateRoutesCommand } from "@aws-sdk/geo-routes-client";
// Import the utility functions
import { withAPIKey } from "@aws/amazon-location-utilities-auth-helper";

// Create an authentication helper instance using an API key and region
const authHelper = withAPIKey("<API Key>", "<Region>");

// Configures the client to use API keys when making supported requests
const client = new GeoRoutesClient(authHelper.getClientConfig());

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

此示例使用 地点 SDK可以提出使用API密钥认证的请求.

npm install @aws-sdk/client-location
// Import from the AWS JavaScript SDK V3 (LocationClient)
import { LocationClient, ListGeofencesCommand } from "@aws-sdk/client-location";
// Import the utility functions
import { withAPIKey } from "@aws/amazon-location-utilities-auth-helper";

// Create an authentication helper instance using an API key and region
const authHelper = withAPIKey("<API Key>", "<Region>");

// Configures the client to use API keys when making supported requests
const client = new LocationClient(authHelper.getClientConfig());

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

此示例使用独立的 路线 SDK要求使用Amazon Cognito认证.

npm install @aws-sdk/geo-routes-client
// Import from the AWS JavaScript SDK V3 (GeoRoutesClient)
import { GeoRoutesClient, CalculateRoutesCommand } from "@aws-sdk/geo-routes-client";
// Import the utility functions
import { withIdentityPoolId } from "@aws/amazon-location-utilities-auth-helper";

// Create an authentication helper instance using credentials from Cognito
const authHelper = await withIdentityPoolId("<Identity Pool ID>");

// Configures the client to use credentials obtained via Amazon Cognito
const client = new GeoRoutesClient(authHelper.getClientConfig());

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

此示例使用独立的 路线 SDK会请求使用自定义的证书提供者进行认证.

npm install @aws-sdk/geo-routes-client
// Import from the AWS JavaScript SDK V3 (GeoRoutesClient)
import { GeoRoutesClient, CalculateRoutesCommand } from "@aws-sdk/geo-routes-client";
// Import the utility functions
import { withCredentialProvider } from "@aws/amazon-location-utilities-auth-helper";

// Create an authentication helper instance using a custom credential provider and region
const authHelper = await withCredentialProvider(async () => {
  // Fetch credentials from your custom source (e.g. a backend service, authenticated Cognito pool, etc.)
  return {
    accessKeyId: "<Access Key ID>",
    secretAccessKey: "<Secret Access Key>",
    sessionToken: "<Session Token>", // optional
    expiration: new Date("<Expiration>"), // optional
  };
}, "<Region>");

// Configures the client to use the custom credentials
const client = new GeoRoutesClient(authHelper.getClientConfig());

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

此示例使用 MapLibre GL 联合来文 以使用自定义的证书提供者来认证资源请求.

// Import MapLibre GL JS
import maplibregl from "maplibre-gl";
// Import the utility function
import { withCredentialProvider } from "@aws/amazon-location-utilities-auth-helper";

const region = "<Region>"; // Region containing Amazon Location resource
const styleName = "Standard"; // Standard, Monochrome, Hybrid, or Satellite

// Create an authentication helper instance using a custom credential provider
const authHelper = await withCredentialProvider(async () => {
  // Fetch credentials from your custom source
  return {
    accessKeyId: "<Access Key ID>",
    secretAccessKey: "<Secret Access Key>",
    sessionToken: "<Session Token>", // optional
    expiration: new Date("<Expiration>"), // optional
  };
}, region);

// Render the map
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(),
});

此示例使用 MapLibre GL 联合来文 以生成使用 API 键认证资源请求的地图.

在使用 MapLibre 来制作使用 API 密钥的地图时不需要认证帮助器,因为样式描述器 URL 和 API 密钥可以直接传递到样式端点.

// Import MapLibre GL JS
import maplibregl from "maplibre-gl";

const apiKey = "<API Key>";
const region = "<Region>"; // Region containing Amazon Location resource
const styleName = "Standard"; // Standard, Monochrome, Hybrid, or Satellite

// Render the map
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}`,
});

此示例使用 MapLibre GL 联合来文 以生成一个使用 Amazon Cognito 验证资源请求的地图.

// Import MapLibre GL JS
import maplibregl from "maplibre-gl";
// Import the utility function
import { withIdentityPoolId } from "@aws/amazon-location-utilities-auth-helper";

const identityPoolId = "<Identity Pool ID>";
const region = "<Region>"; // Region containing Amazon Location resource
const styleName = "Standard"; // Standard, Monochrome, Hybrid, or Satellite

// Create an authentication helper instance using credentials from Cognito
const authHelper = await withIdentityPoolId(identityPoolId);

// Render the map
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(),
});

使用浏览器

用户功能可在 amazonLocationAuthHelper 全球.

其中一些例子使用亚马逊地块客户端. Amazon 位置客户端基于 JavaScript V3 的 AWS SDK 软件 ,并允许通过 HTML 文件引用的脚本给 Amazon Location 打电话.

此示例使用 Amazon 位置客户端来请求使用 API 密钥进行认证 .

<!-- Importing the Amazon Location Client (which includes the auth-helper) -->
<script src="https://cdn.jsdelivr.net/npm/@aws/amazon-location-client@1"></script>
// Create an authentication helper instance using an API key and region
const authHelper = amazonLocationClient.withAPIKey("<API Key>", "<Region>");

// Configures the client to use API keys when making supported requests
const client = new amazonLocationClient.GeoRoutesClient(authHelper.getClientConfig());
const input = { ... };
const command = new amazonLocationClient.routes.CalculateRoutesCommand(input);
const response = await client.send(command);

这个例子使用亚马逊地标客户端来请求使用亚马逊地标认证.

<!-- Import the Amazon Location Client (which includes the auth-helper) -->
<script src="https://cdn.jsdelivr.net/npm/@aws/amazon-location-client@1"></script>
// Create an authentication helper instance using credentials from Cognito
const authHelper = await amazonLocationClient.withIdentityPoolId("<Identity Pool ID>");

// Configures the client to use credentials obtained via Amazon Cognito
const client = new amazonLocationClient.GeoRoutesClient(authHelper.getClientConfig());
const input = { ... };
const command = new amazonLocationClient.routes.CalculateRoutesCommand(input);
const response = await client.send(command);

此示例使用 MapLibre GL 联合来文 以生成使用 API 键认证资源请求的地图.

在使用 MapLibre 来制作使用 API 密钥的地图时不需要认证帮助器,因为样式描述器 URL 和 API 密钥可以直接传递到样式端点.

<!-- MapLibre GL JS -->
<script src="https://cdn.jsdelivr.net/npm/maplibre-gl@5"></script>
const apiKey = "<API Key>";
const region = "<Region>"; // Region containing Amazon Location resource
const styleName = "Standard"; // Standard, Monochrome, Hybrid, or Satellite

// Render the map
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}`,
});

此示例使用 MapLibre GL 联合来文 以生成一个使用 Amazon Cognito 验证资源请求的地图.

<!-- MapLibre GL JS -->
<script src="https://cdn.jsdelivr.net/npm/maplibre-gl@5"></script>
<!-- Importing the authentication SDK -->
<script src="https://cdn.jsdelivr.net/npm/@aws/amazon-location-utilities-auth-helper@1"></script>
const identityPoolId = "<Identity Pool ID>";
const region = "<Region>"; // Region containing Amazon Location resource
const styleName = "Standard"; // Standard, Monochrome, Hybrid, or Satellite

// Create an authentication helper instance using credentials from Cognito
const authHelper = await amazonLocationAuthHelper.withIdentityPoolId(identityPoolId);

// Render the map
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(),
});

或者,如果您喜欢使用认证身份,您可以修改 withIdentityPoolId 用于提供自定义参数的签名 :

const userPoolId = "<User pool Id>";
...

// Create an authentication helper instance using credentials from Cognito
const authHelper = await amazonLocationAuthHelper.withIdentityPoolId(identityPoolId, {
  logins: {
    [`cognito-idp.${region}.amazonaws.com/${userPoolId}`]: "cognito-id-token"
  }
});

...

你可以拿回来 cognito-id-token 从用户会话中 使用扩展

此示例使用 MapLibre GL 联合来文 以使用自定义的证书提供者来认证资源请求.

<!-- MapLibre GL JS -->
<script src="https://cdn.jsdelivr.net/npm/maplibre-gl@5"></script>
<!-- Importing the authentication SDK -->
<script src="https://cdn.jsdelivr.net/npm/@aws/amazon-location-utilities-auth-helper@1"></script>
const region = "<Region>"; // Region containing Amazon Location resource
const styleName = "Standard"; // Standard, Monochrome, Hybrid, or Satellite

// Create an authentication helper instance using a custom credential provider
const authHelper = await amazonLocationAuthHelper.withCredentialProvider(async () => {
  // Fetch credentials from your custom source
  return {
    accessKeyId: "<Access Key ID>",
    secretAccessKey: "<Secret Access Key>",
    sessionToken: "<Session Token>", // optional
    expiration: new Date("<Expiration>"), // optional
  };
}, region);

// Render the map
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(),
});

文档

详细文件可在以下下生成: docs/index.html 通过运行 :

npm run typedoc

withAPIKey

使用 API 密钥( 和区域, 可选) 创建 Auth 帮助实例 .

const authHelper = withAPIKey(apiKey, region);

withIdentityPoolId

使用来自 Cognitto 的证书创建认证帮助实例 .

const authHelper = await withIdentityPoolId(identityPoolId);

withCredentialProvider

使用任意性创建认证助手实例 证书提供者 函数。 在通过自定义机制获得证书时,例如通过专门的后端服务获得明确的AWS证书时,这样做是有用的.

const authHelper = await withCredentialProvider(credentialProvider, region);

获取帮助

与我们团队互动的最佳方式是通过GitHub. 你们可以 打开一个问题 从我们的模板中选择一个 错误报告, (中文(简体) ), 特性请求 或者说 指导。 。 。 . 如果您有支持计划 AWS 支持,也可以创建新的支持案件.

请在开始发行前检查以下资源:

捐款

我们欢迎社区的贡献并征求他们的意见。 见 捐款额 关于如何建立开发环境和提交代码的信息.

许可证

Amazon 位置工具 - JavaScript 的认证帮助器分布在 Apache 许可证,2.0版本, (中文(简体) ), 更多信息见LICENSE.txt和Notice.txt.

版本与发布记录

@aws/amazon-location-utilities-auth-helper 共发布 19 个版本,首次发布于 2023/07/04;过去 12 个月发布了 5 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.3.3latest2026/05/096 个月前
1.3.22026/04/296 个月前
1.3.12026/03/257 个月前
1.3.02026/02/198 个月前
1.2.52026/02/118 个月前
1.2.42025/10/231 年前

关于 @aws/amazon-location-utilities-auth-helper 的常见问题

@aws/amazon-location-utilities-auth-helper 是什么?

Amazon 位置工具 - JavaScript 认证帮助器

如何安装 @aws/amazon-location-utilities-auth-helper?

在项目目录运行 npm install @aws/amazon-location-utilities-auth-helper,或使用 pnpm add / yarn add / bun add。

@aws/amazon-location-utilities-auth-helper 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@aws/amazon-location-utilities-auth-helper 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@aws/amazon-location-utilities-auth-helper 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 20.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@aws/amazon-location-utilities-auth-helper 可以商用吗?

@aws/amazon-location-utilities-auth-helper 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@aws/amazon-location-utilities-auth-helper 的下载量有多少?

根据 npm 官方下载统计,@aws/amazon-location-utilities-auth-helper 在 2026/10/08 – 2026/11/06 这 30 天内被下载 213,139 次,日均约 7,105 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@aws/amazon-location-utilities-auth-helper 还在维护吗?

最新版本 1.3.3 发布于 2026/05/09(6 个月前);至今共发布 19 个版本,其中过去 12 个月发布了 5 个;首次发布于 2023/07/04。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。