createToken method

Future<CreateTokenResponse> createToken({
  1. required String clientId,
  2. required String clientSecret,
  3. required String grantType,
  4. String? code,
  5. String? codeVerifier,
  6. String? deviceCode,
  7. String? redirectUri,
  8. String? refreshToken,
  9. List<String>? scope,
})

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.

May throw AccessDeniedException. May throw AuthorizationPendingException. May throw ExpiredTokenException. May throw InternalServerException. May throw InvalidClientException. May throw InvalidGrantException. May throw InvalidRequestException. May throw InvalidScopeException. May throw SlowDownException. May throw UnauthorizedClientException. May throw UnsupportedGrantTypeException.

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

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

Parameter 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

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

Parameter codeVerifier : 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.

Parameter deviceCode : 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.

Parameter redirectUri : 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.

Parameter refreshToken : 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.

Parameter scope : 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.

Implementation

Future<CreateTokenResponse> createToken({
  required String clientId,
  required String clientSecret,
  required String grantType,
  String? code,
  String? codeVerifier,
  String? deviceCode,
  String? redirectUri,
  String? refreshToken,
  List<String>? scope,
}) async {
  final $payload = <String, dynamic>{
    'clientId': clientId,
    'clientSecret': clientSecret,
    'grantType': grantType,
    if (code != null) 'code': code,
    if (codeVerifier != null) 'codeVerifier': codeVerifier,
    if (deviceCode != null) 'deviceCode': deviceCode,
    if (redirectUri != null) 'redirectUri': redirectUri,
    if (refreshToken != null) 'refreshToken': refreshToken,
    if (scope != null) 'scope': scope,
  };
  final response = await _protocol.send(
    payload: $payload,
    method: 'POST',
    requestUri: '/token',
    signed: false,
    exceptionFnMap: _exceptionFns,
  );
  return CreateTokenResponse.fromJson(response);
}