refreshAccessToken static method

Future<OAuthResponse> refreshAccessToken({
  1. required String clientId,
  2. required String clientSecret,
  3. required String refreshToken,
})

Reissue the access token using the refresh token.

Implementation

static Future<OAuthResponse> refreshAccessToken({
  required String clientId,
  required String clientSecret,
  required String refreshToken,
}) async {
  final credentials = base64.encode(utf8.encode('$clientId:$clientSecret'));

  final response = await http.post(
    Uri.https('api.twitter.com', '/2/oauth2/token'),
    headers: {
      'Authorization': 'Basic $credentials',
    },
    body: {
      'grant_type': 'refresh_token',
      'refresh_token': refreshToken,
    },
  );

  if (response.statusCode != 200) {
    throw TwitterException('Failed to refresh an access token.', response);
  }

  return OAuthResponse.fromJson(
    jsonDecode(response.body),
  );
}