令牌端点 - Amazon Cognito
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

令牌端点

生成发送到 /oauth2/token 端点的 POST 请求,以便为用户或服务获取 JSON Web 令牌(JWT,JSON Web Token)。当您向用户群体添加域时,Amazon Cognito 会激活专用于您的用户群体的 OAuth 2.0 令牌端点。在基于用户的模型中,您的应用程序向令牌端点发送授权代码,用于交换 ID、访问权限和刷新令牌。在机器对机器的模型中,您的应用程序向令牌端点发送客户端密钥,用于交换访问令牌。

要从用户的访问令牌中检索有关用户的信息,您可以将其传递给 UserInfo 终端节点GetUser API 请求。

POST /oauth2/token

/oauth2/token 终端节点只支持 HTTPS POST。您的应用程序直接对此端点发出请求,而不通过用户浏览器。

令牌端点支持 client_secret_basicclient_secret_post 身份验证。有关 OpenID Connect 规范的更多信息,请参阅客户端身份验证。有关 OpenID Connect 规范中令牌端点的更多信息,请参阅令牌端点

标头中的请求参数

Authorization

如果向客户端发布了密钥,则客户端可以在授权标头中将其 client_idclient_secret 作为 client_secret_basic HTTP 授权传递。您还可以在请求正文中包含 client_idclient_secret 作为 client_secret_post 授权。

授权标头字符串是 基本 Base64Encode(client_id:client_secret)。以下示例是具有客户端密钥 abcdef01234567890 的应用程序客户端 djc98u3jiedmi283eu928 的授权标头,其使用字符串 djc98u3jiedmi283eu928:abcdef01234567890 的 Base64 编码版本。

Authorization: Basic ZGpjOTh1M2ppZWRtaTI4M2V1OTI4OmFiY2RlZjAxMjM0NTY3ODkw
Content-Type

必须始终为 'application/x-www-form-urlencoded'

正文中的请求参数

grant_type

授予类型。

必须为 authorization_coderefresh_tokenclient_credentials。当在应用程序客户端中已启用请求的范围、已配置客户端密钥并且具有允许的 client_credentials 授予时,您可以通过令牌端点为自定义范围请求访问令牌。

必需。

client_id

用户群体中应用程序客户端的 ID。必须指定对用户进行身份验证的同一个应用程序客户端。

如果客户端是公有的且没有密钥,则为必需的;或者,如果想要使用 client_secret_post 授权,则使用 client_secret

client_secret

对用户进行身份验证的应用程序客户端的客户端密钥。如果您的应用程序客户端具有客户端密钥,并且您未发送 Authorization 标头,则为必需的。

scope

可以是与应用程序客户端关联的任何自定义范围的组合。必须为应用程序客户端激活您请求的任何范围,否则 Amazon Cognito 将忽略它。如果客户端不请求任何范围,则身份验证服务器使用与客户端关联的所有自定义范围。

可选。仅当 grant_typeclient_credentials 时使用。

redirect_uri

必须是用于在 /oauth2/authorize 中获取 authorization_code 的相同 redirect_uri

仅当 grant_typeauthorization_code 时必需。

refresh_token

要为用户的会话生成新的访问令牌和 ID 令牌,请将您的 /oauth2/token 请求中的 refresh_token 参数的值设置为同一个应用程序客户端中以前颁发的刷新令牌。

代码

如果 grant_typeauthorization_code,则必需。

code_verifier

证明密钥。

如果 grant_typeauthorization_code 并且使用 PKCE 请求了授权代码,则必需。

具有正向响应的示例请求

为获取令牌交换授权代码

示例请求

POST https://mydomain.auth.us-east-1.amazoncognito.com/oauth2/token& Content-Type='application/x-www-form-urlencoded'& Authorization=Basic ZGpjOTh1M2ppZWRtaTI4M2V1OTI4OmFiY2RlZjAxMjM0NTY3ODkw grant_type=authorization_code& client_id=1example23456789& code=AUTHORIZATION_CODE& redirect_uri=com.myclientapp://myclient/redirect

示例响应

HTTP/1.1 200 OK Content-Type: application/json { "access_token":"eyJra1example", "id_token":"eyJra2example", "refresh_token":"eyJj3example", "token_type":"Bearer", "expires_in":3600 }
注意

仅当 grant_typeauthorization_code 时,令牌终端节点才返回 refresh_token

为获取访问令牌交换客户端凭证

示例请求

POST https://mydomain.auth.us-east-1.amazoncognito.com/oauth2/token > Content-Type='application/x-www-form-urlencoded'& Authorization=Basic ZGpjOTh1M2ppZWRtaTI4M2V1OTI4OmFiY2RlZjAxMjM0NTY3ODkw grant_type=client_credentials& client_id=1example23456789& scope={resourceServerIdentifier1}/{scope1} {resourceServerIdentifier2}/{scope2}

示例响应

HTTP/1.1 200 OK Content-Type: application/json { "access_token":"eyJra1example", "token_type":"Bearer", "expires_in":3600 }

为获取令牌交换具有 PKCE 的授权代码授予

示例请求

POST https://mydomain.auth.us-east-1.amazoncognito.com/oauth2/token Content-Type='application/x-www-form-urlencoded'& Authorization=Basic ZGpjOTh1M2ppZWRtaTI4M2V1OTI4OmFiY2RlZjAxMjM0NTY3ODkw grant_type=authorization_code& client_id=1example23456789& code=AUTHORIZATION_CODE& code_verifier=CODE_VERIFIER& redirect_uri=com.myclientapp://myclient/redirect

示例响应

HTTP/1.1 200 OK Content-Type: application/json { "access_token":"eyJra1example", "id_token":"eyJra2example", "refresh_token":"eyJj3example", "token_type":"Bearer", "expires_in":3600 }
注意

仅当 grant_typeauthorization_code 时,令牌终端节点才返回 refresh_token

为获取令牌交换刷新令牌

示例请求

POST https://mydomain.auth.us-east-1.amazoncognito.com/oauth2/token > Content-Type='application/x-www-form-urlencoded'& Authorization=Basic ZGpjOTh1M2ppZWRtaTI4M2V1OTI4OmFiY2RlZjAxMjM0NTY3ODkw grant_type=refresh_token& client_id=1example23456789& refresh_token=eyJj3example

示例响应

HTTP/1.1 200 OK Content-Type: application/json { "access_token":"eyJra1example", "id_token":"eyJra2example", "token_type":"Bearer", "expires_in":3600 }
注意

仅当 grant_typeauthorization_code 时,令牌终端节点才返回 refresh_token

负向响应的示例

错误响应示例

HTTP/1.1 400 Bad Request Content-Type: application/json;charset=UTF-8 { "error":"invalid_request|invalid_client|invalid_grant|unauthorized_client|unsupported_grant_type" }
invalid_request

请求缺少必需的参数、包括不支持的参数值 (除了 unsupported_grant_type 之外) 或者格式错误。例如,grant_typerefresh_token,但未包括 refresh_token

invalid_client

客户端身份验证失败。例如,客户端的授权标头中包含 client_idclient_secret,但没有这样的客户端带有 client_idclient_secret

invalid_grant

已撤销刷新令牌。

授权代码已使用或不存在。

应用程序客户端对请求的范围内的所有属性都没有读取权限。例如,您的应用程序请求 email 范围,应用程序客户端可以读取 email 属性,但不能读取 email_verified

unauthorized_client

客户端不允许代码授予流或刷新令牌。

unsupported_grant_type

如果 grant_typeauthorization_coderefresh_tokenclient_credentials 之外的任意内容,则返回。