{"record":{"id":"af56c0dd339395a4","repo":"zeroclaw-labs/zeroclaw","slug":"xai-device-code-start-failed-status-body","errorCode":null,"errorMessage":"xAI device-code start failed ({status}): {body}","messagePattern":"xAI device-code start failed \\((.+?)\\): (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/zeroclaw-providers/src/auth/xai_oauth.rs","lineNumber":244,"sourceCode":"    let form = [\n        (\"client_id\", XAI_OAUTH_CLIENT_ID),\n        (\"scope\", XAI_OAUTH_SCOPE),\n    ];\n\n    let response = client\n        .post(require_trusted_endpoint(\n            device_authorization_endpoint,\n            \"device authorization endpoint\",\n        )?)\n        .form(&form)\n        .send()\n        .await\n        .context(\"Failed to start xAI OAuth device-code flow\")?;\n\n    if !response.status().is_success() {\n        let status = response.status();\n        let body = response.text().await.unwrap_or_default();\n        anyhow::bail!(\"xAI device-code start failed ({status}): {body}\");\n    }\n\n    let parsed: DeviceCodeResponse = response\n        .json()\n        .await\n        .context(\"Failed to parse xAI device-code response\")?;\n    Ok(DeviceCodeStart {\n        device_code: parsed.device_code,\n        user_code: parsed.user_code,\n        verification_uri: require_trusted_endpoint(&parsed.verification_uri, \"verification URI\")?,\n        verification_uri_complete: parsed\n            .verification_uri_complete\n            .as_deref()\n            .map(|uri| require_trusted_endpoint(uri, \"complete verification URI\"))\n            .transpose()?,\n        expires_in: parsed.expires_in,\n        interval: parsed.interval.unwrap_or(5).max(1),\n    })","sourceCodeStart":226,"sourceCodeEnd":262,"githubUrl":"https://github.com/zeroclaw-labs/zeroclaw/blob/88bb9c8533fc57ed7a03e36ca7c9ed2bf8336dcc/crates/zeroclaw-providers/src/auth/xai_oauth.rs#L226-L262","documentation":"`start_device_code_flow` POSTs client_id and scope to the xAI device authorization endpoint and received a non-2xx. This is the first step of device-code login — nothing has been shown to the user yet. Status and body are embedded; common bodies are `invalid_client`, `unauthorized_client`, or rate-limit text.","triggerScenarios":"Calling `start_device_code_flow` with a discovered device_authorization_endpoint that rejects the request; xAI disabling device grants for the shared client; hammering the endpoint past rate limits in scripts.","commonSituations":"Provider-side policy changes to the device flow; automated tests repeatedly starting flows; regional blocks on xAI auth.","solutions":["Read the embedded body: `invalid_client`/`unauthorized_client` mean device flow is unavailable for this client — switch to the browser loopback flow (`receive_loopback_code` on 127.0.0.1:56121)","Back off and retry once for 429/5xx","Confirm discovery succeeded and supplied a device_authorization_endpoint on an x.ai host"],"exampleFix":"// before: device flow only\nlet device = start_device_code_flow(&client, &disc.device_authorization_endpoint).await?;\n\n// after: fall back to the browser flow when device start is rejected\nlet device = match start_device_code_flow(&client, &disc.device_authorization_endpoint).await {\n    Ok(d) => d,\n    Err(_) => return browser_login(&client).await,\n};","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"let device = match start_device_code_flow(&client, &disc.device_authorization_endpoint).await {\n    Ok(d) => d,\n    Err(e) if e.to_string().contains(\"device-code start failed\") => {\n        // device grant unavailable for this client: switch to the browser loopback flow\n        return browser_login(&client).await;\n    }\n    Err(e) => return Err(e),\n};","preventionTips":["Rate-limit device-flow starts in scripts and tests","Prefer the browser loopback flow where a display is available","Verify discovery endpoints are x.ai hosts via require_trusted_endpoint before use"],"tags":["oauth","xai","device-code","http","rust"],"backgroundTag":"oauth-device-code-start-failed","analyzedSha":"88bb9c8533fc57ed7a03e36ca7c9ed2bf8336dcc","analyzedAt":"2026-08-23T01:07:41.857Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}