requestDeviceCode method

Future<DeviceCodeChallenge> requestDeviceCode()

Implementation

Future<DeviceCodeChallenge> requestDeviceCode() async {
  debugPrint('[OpenAiAuth] Requesting device code');

  final response = await _dio.post(
    '$_issuer/api/accounts/deviceauth/usercode',
    options: Options(headers: {'content-type': 'application/json'}),
    data: {'client_id': _clientId},
  );

  final data = response.data as Map<String, dynamic>;
  debugPrint('[OpenAiAuth] usercode response keys: ${data.keys.toList()}');

  final deviceAuthId = data['device_auth_id'] as String;
  final userCode = (data['user_code'] ?? data['usercode']) as String;
  final rawInterval = data['interval'];
  final interval = rawInterval is int
      ? rawInterval
      : int.tryParse(rawInterval.toString()) ?? 5;

  debugPrint('[OpenAiAuth] Got code=$userCode, interval=$interval');

  return DeviceCodeChallenge(
    deviceAuthId: deviceAuthId,
    userCode: userCode,
    verificationUrl: '$_issuer/codex/device',
    intervalSeconds: interval,
  );
}