Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅
中国的 Amazon Web Services 服务入门
(PDF)。
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
GetClientToken搭配使用 Amazon SDK
以下代码示例演示了如何使用 GetClientToken。
操作示例是大型程序的代码摘录,必须在上下文中运行。在以下代码示例中,您可以查看此操作的上下文:
- Python
-
- 适用于 Python 的 SDK(Boto3)
-
def get_client_token(
self,
client_id: str,
client_secret: str,
scopes: Optional[list[str]] = None,
) -> dict[str, Any]:
"""
Obtains an M2M access token for an app client using its client ID
and secret, without end-user sign-in.
:param client_id: The ID of the app client.
:param client_secret: The client secret for the app client.
:param scopes: Optional list of scopes in the format
'resource-server-identifier/scope-name'. If omitted,
all scopes configured for the app client are authorized.
:return: A dictionary containing the ClientAuthenticationResult with
AccessToken, ExpiresIn, and TokenType.
"""
try:
params = dict()
params["ClientId"] = client_id
params["Secret"] = client_secret
if scopes is not None:
params["Scopes"] = scopes
response = self.cognito_idp_client.get_client_token(**params)
auth_result = response.get("ClientAuthenticationResult", dict())
logger.info(
"Obtained M2M access token for client %s. Token type: %s, expires in: %s seconds.",
client_id,
auth_result.get("TokenType", "Unknown"),
auth_result.get("ExpiresIn", 0),
)
return auth_result
except ClientError as err:
if err.response["Error"]["Code"] == "NotAuthorizedException":
logger.error(
"Not authorized for client %s. Verify that the client ID and "
"secret are correct, and that the ALLOW_CLIENT_TOKEN_AUTH flow "
"is enabled for the app client.",
client_id,
)
raise
有关 S Amazon DK 开发者指南和代码示例的完整列表,请参阅将此服务与 Amazon SDK。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。