Skip to content

create_token

create_token(
    client_id: ClientId,
    client_secret: ClientSecret,
    grant_type: GrantType,
    *,
    config_overrides: Optional[SSOOIDCClientConfig] = None,
    device_code: Optional[DeviceCode] = None,
    code: Optional[AuthCode] = None,
    refresh_token: Optional[RefreshToken] = None,
    scope: Optional[Scopes] = None,
    redirect_uri: Optional[URI] = None,
    code_verifier: Optional[CodeVerifier] = None,
) -> CreateTokenResponse

Creates and returns access and refresh tokens for clients that are authenticated using client secrets. The access token can be used to fetch short-lived credentials for the assigned AWS accounts or to access application APIs using bearer authentication.

Parameters:

  • client_id (ClientId) –

    The unique identifier string for the client or application. This value comes from the result of the RegisterClient API.

  • client_secret (ClientSecret) –

    A secret string generated for the client. This value should come from the persisted result of the RegisterClient API.

  • grant_type (GrantType) –

    Supports the following OAuth grant types: Authorization Code, Device Code, and Refresh Token. Specify one of the following values, depending on the grant type that you want:

    * Authorization Code - authorization_code

    * Device Code - urn:ietf:params:oauth:grant-type:device_code

    * Refresh Token - refresh_token

  • device_code (Optional[DeviceCode], default: None ) –

    Used only when calling this API for the Device Code grant type. This short-lived code is used to identify this authorization request. This comes from the result of the StartDeviceAuthorization API.

  • code (Optional[AuthCode], default: None ) –

    Used only when calling this API for the Authorization Code grant type. The short-lived code is used to identify this authorization request.

  • refresh_token (Optional[RefreshToken], default: None ) –

    Used only when calling this API for the Refresh Token grant type. This token is used to refresh short-lived tokens, such as the access token, that might expire.

    For more information about the features and limitations of the current IAM Identity Center OIDC implementation, see Considerations for Using this Guide in the IAM Identity Center OIDC API Reference.

  • scope (Optional[Scopes], default: None ) –

    The list of scopes for which authorization is requested. This parameter has no effect; the access token will always include all scopes configured during client registration.

  • redirect_uri (Optional[URI], default: None ) –

    Used only when calling this API for the Authorization Code grant type. This value specifies the location of the client or application that has registered to receive the authorization code.

  • code_verifier (Optional[CodeVerifier], default: None ) –

    Used only when calling this API for the Authorization Code grant type. This value is generated by the client and presented to validate the original code challenge value the client passed at authorization time.

Raises:

  • AccessDeniedException –

    You do not have sufficient access to perform this action.

  • AuthorizationPendingException –

    Indicates that a request to authorize a client with an access user session token is pending.

  • ExpiredTokenException –

    Indicates that the token issued by the service is expired and is no longer valid.

  • InternalServerException –

    Indicates that an error from the service occurred while trying to process a request.

  • InvalidClientException –

    Indicates that the clientId or clientSecret in the request is invalid. For example, this can occur when a client sends an incorrect clientId or an expired clientSecret.

  • InvalidGrantException –

    Indicates that a request contains an invalid grant. This can occur if a client makes a CreateToken request with an invalid grant type.

  • InvalidRequestException –

    Indicates that something is wrong with the input to the request. For example, a required parameter might be missing or out of range.

  • InvalidScopeException –

    Indicates that the scope provided in the request is invalid.

  • SlowDownException –

    Indicates that the client is making the request too frequently and is more than the service can handle.

  • UnauthorizedClientException –

    Indicates that the client is not currently authorized to make the request. This can happen when a clientId is not issued for a public client.

  • UnsupportedGrantTypeException –

    Indicates that the grant type in the request is not supported by the service.

  • UnknownServiceError –

    The service returned an error code this client does not model.

Examples:

Call OAuth/OIDC /token endpoint for Device Code grant with Secret authentication

>>> client.create_token(client_id='_yzkThXVzLWVhc3QtMQEXAMPLECLIENTID', client_secret='VERYLONGSECRETeyJraWQiOiJrZXktMTU2NDAyODA5OSIsImFsZyI6IkhTMzg0In0', grant_type='urn:ietf:params:oauth:grant-type:device-code', device_code='yJraWQiOiJrZXktMTU2Njk2ODA4OCIsImFsZyI6IkhTMzIn0EXAMPLEDEVICECODE')
Call OAuth/OIDC /token endpoint for Refresh Token grant with Secret authentication
>>> client.create_token(client_id='_yzkThXVzLWVhc3QtMQEXAMPLECLIENTID', client_secret='VERYLONGSECRETeyJraWQiOiJrZXktMTU2NDAyODA5OSIsImFsZyI6IkhTMzg0In0', grant_type='refresh_token', refresh_token='aorvJYubGpU6i91YnH7Mfo-AT2fIVa1zCfA_Rvq9yjVKIP3onFmmykuQ7E93y2I-9Nyj-A_sVvMufaLNL0bqnDRtgAkc0:MGUCMFrRsktMRVlWaOR70XGMFGLL0SlcCw4DiYveIiOVx1uK9BbD0gvAddsW3UTLozXKMgIxAJ3qxUvjpnlLIOaaKOoa/FuNgqJVvr9GMwDtnAtlh9iZzAkEXAMPLEREFRESHTOKEN', scope=['codewhisperer:completions'])
Source code in docs_src/capo_sso_oidc/_services/ssooidc.py
def create_token(
    self,
    client_id: "capo_sso_oidc.types.client_id.ClientId",
    client_secret: "capo_sso_oidc.types.client_secret.ClientSecret",
    grant_type: "capo_sso_oidc.types.grant_type.GrantType",
    *,
    config_overrides: Optional[SSOOIDCClientConfig] = None,
    device_code: Optional["capo_sso_oidc.types.device_code.DeviceCode"] = None,
    code: Optional["capo_sso_oidc.types.auth_code.AuthCode"] = None,
    refresh_token: Optional[
        "capo_sso_oidc.types.refresh_token.RefreshToken"
    ] = None,
    scope: Optional["capo_sso_oidc.types.scopes.Scopes"] = None,
    redirect_uri: Optional["capo_sso_oidc.types.uri.URI"] = None,
    code_verifier: Optional[
        "capo_sso_oidc.types.code_verifier.CodeVerifier"
    ] = None,
) -> "capo_sso_oidc.types.create_token_response.CreateTokenResponse":
    """<p>Creates and returns access and refresh tokens for clients that are authenticated using client secrets. The access token can be used to fetch short-lived credentials for the assigned AWS accounts or to access application APIs using <code>bearer</code> authentication.</p>

    Args:
        client_id: <p>The unique identifier string for the client or application. This value comes from the result of the <a>RegisterClient</a> API.</p>
        client_secret: <p>A secret string generated for the client. This value should come from the persisted result of the <a>RegisterClient</a> API.</p>
        grant_type: <p>Supports the following OAuth grant types: Authorization Code, Device Code, and Refresh Token. Specify one of the following values, depending on the grant type that you want:</p> <p>* Authorization Code - <code>authorization_code</code> </p> <p>* Device Code - <code>urn:ietf:params:oauth:grant-type:device_code</code> </p> <p>* Refresh Token - <code>refresh_token</code> </p>
        device_code: <p>Used only when calling this API for the Device Code grant type. This short-lived code is used to identify this authorization request. This comes from the result of the <a>StartDeviceAuthorization</a> API.</p>
        code: <p>Used only when calling this API for the Authorization Code grant type. The short-lived code is used to identify this authorization request.</p>
        refresh_token: <p>Used only when calling this API for the Refresh Token grant type. This token is used to refresh short-lived tokens, such as the access token, that might expire.</p> <p>For more information about the features and limitations of the current IAM Identity Center OIDC implementation, see <i>Considerations for Using this Guide</i> in the <a href="https://docs.aws.amazon.com/singlesignon/latest/OIDCAPIReference/Welcome.html">IAM Identity Center OIDC API Reference</a>.</p>
        scope: <p>The list of scopes for which authorization is requested. This parameter has no effect; the access token will always include all scopes configured during client registration.</p>
        redirect_uri: <p>Used only when calling this API for the Authorization Code grant type. This value specifies the location of the client or application that has registered to receive the authorization code.</p>
        code_verifier: <p>Used only when calling this API for the Authorization Code grant type. This value is generated by the client and presented to validate the original code challenge value the client passed at authorization time.</p>

    Raises:
        capo_sso_oidc.errors.access_denied_exception.AccessDeniedException: <p>You do not have sufficient access to perform this action.</p>
        capo_sso_oidc.errors.authorization_pending_exception.AuthorizationPendingException: <p>Indicates that a request to authorize a client with an access user session token is pending.</p>
        capo_sso_oidc.errors.expired_token_exception.ExpiredTokenException: <p>Indicates that the token issued by the service is expired and is no longer valid.</p>
        capo_sso_oidc.errors.internal_server_exception.InternalServerException: <p>Indicates that an error from the service occurred while trying to process a request.</p>
        capo_sso_oidc.errors.invalid_client_exception.InvalidClientException: <p>Indicates that the <code>clientId</code> or <code>clientSecret</code> in the request is invalid. For example, this can occur when a client sends an incorrect <code>clientId</code> or an expired <code>clientSecret</code>.</p>
        capo_sso_oidc.errors.invalid_grant_exception.InvalidGrantException: <p>Indicates that a request contains an invalid grant. This can occur if a client makes a <a>CreateToken</a> request with an invalid grant type.</p>
        capo_sso_oidc.errors.invalid_request_exception.InvalidRequestException: <p>Indicates that something is wrong with the input to the request. For example, a required parameter might be missing or out of range.</p>
        capo_sso_oidc.errors.invalid_scope_exception.InvalidScopeException: <p>Indicates that the scope provided in the request is invalid.</p>
        capo_sso_oidc.errors.slow_down_exception.SlowDownException: <p>Indicates that the client is making the request too frequently and is more than the service can handle. </p>
        capo_sso_oidc.errors.unauthorized_client_exception.UnauthorizedClientException: <p>Indicates that the client is not currently authorized to make the request. This can happen when a <code>clientId</code> is not issued for a public client.</p>
        capo_sso_oidc.errors.unsupported_grant_type_exception.UnsupportedGrantTypeException: <p>Indicates that the grant type in the request is not supported by the service.</p>
        capo_sso_oidc.errors.UnknownServiceError: The service returned an error code this client does not model.

    Examples:
        Call OAuth/OIDC /token endpoint for Device Code grant with Secret authentication

        >>> client.create_token(client_id='_yzkThXVzLWVhc3QtMQEXAMPLECLIENTID', client_secret='VERYLONGSECRETeyJraWQiOiJrZXktMTU2NDAyODA5OSIsImFsZyI6IkhTMzg0In0', grant_type='urn:ietf:params:oauth:grant-type:device-code', device_code='yJraWQiOiJrZXktMTU2Njk2ODA4OCIsImFsZyI6IkhTMzIn0EXAMPLEDEVICECODE')
        Call OAuth/OIDC /token endpoint for Refresh Token grant with Secret authentication

        >>> client.create_token(client_id='_yzkThXVzLWVhc3QtMQEXAMPLECLIENTID', client_secret='VERYLONGSECRETeyJraWQiOiJrZXktMTU2NDAyODA5OSIsImFsZyI6IkhTMzg0In0', grant_type='refresh_token', refresh_token='aorvJYubGpU6i91YnH7Mfo-AT2fIVa1zCfA_Rvq9yjVKIP3onFmmykuQ7E93y2I-9Nyj-A_sVvMufaLNL0bqnDRtgAkc0:MGUCMFrRsktMRVlWaOR70XGMFGLL0SlcCw4DiYveIiOVx1uK9BbD0gvAddsW3UTLozXKMgIxAJ3qxUvjpnlLIOaaKOoa/FuNgqJVvr9GMwDtnAtlh9iZzAkEXAMPLEREFRESHTOKEN', scope=['codewhisperer:completions'])
    """

    def _handler(
        req: "OperationRequest[capo_sso_oidc.types.create_token_request.CreateTokenRequest]",
    ) -> OperationResponse[
        "capo_sso_oidc.types.create_token_response.CreateTokenResponse"
    ]:
        import capo_sso_oidc._operations.awsssooidc_service.create_token

        output, http_response = (
            capo_sso_oidc._operations.awsssooidc_service.create_token.create_token(
                req.options, req.input
            )
        )
        return OperationResponse(output=output, response=http_response)

    def _redact_input(
        input_: "capo_sso_oidc.types.create_token_request.CreateTokenRequest",
    ) -> dict[str, Any]:
        import capo_sso_oidc.types.create_token_request

        return capo_sso_oidc.types.create_token_request.redact_sensitive(input_)

    def _redact_output(
        output: "capo_sso_oidc.types.create_token_response.CreateTokenResponse",
    ) -> dict[str, Any]:
        import capo_sso_oidc.types.create_token_response

        return capo_sso_oidc.types.create_token_response.redact_sensitive(output)

    interceptors_, options_ = self.operation_options(
        config_overrides,
        OperationLogging(
            "CreateToken", redact_input=_redact_input, redact_output=_redact_output
        ),
    )
    input_: capo_sso_oidc.types.create_token_request.CreateTokenRequest = {
        "client_id": client_id,
        "client_secret": client_secret,
        "grant_type": grant_type,
    }
    if device_code is not None:
        input_["device_code"] = device_code
    if code is not None:
        input_["code"] = code
    if refresh_token is not None:
        input_["refresh_token"] = refresh_token
    if scope is not None:
        input_["scope"] = scope
    if redirect_uri is not None:
        input_["redirect_uri"] = redirect_uri
    if code_verifier is not None:
        input_["code_verifier"] = code_verifier

    response = execute_pipeline(
        OperationRequest(input=input_, options=options_),
        handler=_handler,
        interceptors=list(interceptors_),
    )
    response.response.close()
    return response.output