Amazon 位置工具 - JavaScript 认证帮助器
这些是公用设备 用来帮助客户验证 亚马逊定位服务 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.