exchangeCodeForTokens method

Future<Map<String, dynamic>?> exchangeCodeForTokens(
  1. String code,
  2. String clientId,
  3. String clientSecret,
  4. String redirectUri,
  5. String tokenEndpoint,
)

Implementation

Future<Map<String, dynamic>?> exchangeCodeForTokens(
    String code,
    String clientId,
    String clientSecret,
    String redirectUri,
    String tokenEndpoint) async {
  print('Function Exchange Code For Tokens');
  try {
    final response = await _dio.post(
      tokenEndpoint,
      data: {
        'code': code,
        'client_id': clientId,
        'client_secret': clientSecret,
        'redirect_uri': redirectUri,
        'grant_type': 'authorization_code',
      },
      options: Options(
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
        },
      ),
    );

    if (response.statusCode == 200) {
      return response.data;
    } else {
      print(
          'Error exchanging code for tokens: ${response.statusCode} ${response.data}');
      return null;
    }
  } catch (e) {
    print('Error exchanging code for tokens: $e');
    if (e is DioError) {
      print('Request data: ${e.requestOptions.data}');
      print('Response data: ${e.response?.data}');
    }
    return null;
  }
}