getMinecraftAccessTokenWithDeviceCode method

  1. @override
Future<MinecraftTokenResponse> getMinecraftAccessTokenWithDeviceCode(
  1. String deviceCode
)
override

Implementation

@override
Future<MinecraftTokenResponse> getMinecraftAccessTokenWithDeviceCode(
  String deviceCode,
) async {
  try {
    final response = await _httpClient.post(
      Uri.parse(
        'https://login.microsoftonline.com/consumers/oauth2/v2.0/token',
      ),
      headers: {'Content-Type': 'application/x-www-form-urlencoded'},
      body: {
        'client_id': _clientId,
        'device_code': deviceCode,
        'grant_type': 'urn:ietf:params:oauth:grant-type:device_code',
      },
    );

    if (response.statusCode == 200) {
      final jsonResponse = jsonDecode(response.body);
      return MinecraftTokenResponse.fromJson(jsonResponse);
    } else {
      throw Exception('Device code authentication failed: ${response.body}');
    }
  } catch (e) {
    throw Exception('Failed to retrieve token with device code: $e');
  }
}