larksuite/cli · error

app registration failed: response missing device_code

Error message

app registration failed: response missing device_code

What it means

Thrown by RequestAppRegistration at internal/auth/app_registration.go:154 when the begin response parses as JSON, has no error field and status < 400, but the device_code field is missing or empty. Without a device_code the device authorization flow cannot proceed, so the library treats the response as an invalid protocol payload.

Source

Thrown at internal/auth/app_registration.go:154

		}
		if msg == "" {
			msg = "Unknown error"
		}
		return nil, fmt.Errorf("app registration failed: %s", msg)
	}

	// The protocol field is expire_in; accept the legacy expires_in spelling,
	// then normalize to protocol defaults.
	expiresIn := getInt(data, "expire_in", 0)
	if expiresIn <= 0 {
		expiresIn = getInt(data, "expires_in", 0)
	}
	expiresIn = normalizedExpireIn(expiresIn)
	interval := normalizedInterval(getInt(data, "interval", 0))

	deviceCode := getStr(data, "device_code")
	if deviceCode == "" {
		return nil, fmt.Errorf("app registration failed: response missing device_code")
	}

	userCode := getStr(data, "user_code")
	verificationUri := getStr(data, "verification_uri")
	verificationUriComplete := fmt.Sprintf("%s/page/cli?user_code=%s", ep.Open, userCode)

	return &AppRegistrationResponse{
		DeviceCode:              deviceCode,
		UserCode:                getStr(data, "user_code"),
		VerificationUri:         verificationUri,
		VerificationUriComplete: verificationUriComplete,
		ExpiresIn:               expiresIn,
		Interval:                interval,
	}, nil
}

// BuildVerificationURL appends CLI tracking parameters to the verification URL.
func BuildVerificationURL(baseURL, cliVersion string) string {

View on GitHub (pinned to 7fd6ef3c07)

Solutions

  1. Retry — a transient server bug may have returned a malformed payload.
  2. Confirm you are on the current CLI version so the request matches the server protocol.
  3. Bypass proxies/inspection appliances that may rewrite response JSON.
  4. Verify the accounts registration endpoint directly with curl to inspect the raw JSON body.
  5. If persistent, file a bug with the CLI maintainers including the HTTP status and sanitized response.
Defensive patterns

Strategy: type-guard

Validate before calling

func isValidRegistrationResponse(r *AppRegistrationResponse) bool {
    return r != nil && r.DeviceCode != ""
}

Type guard

func hasDeviceCode(r *AppRegistrationResponse) bool { return r != nil && r.DeviceCode != "" }

Try / catch

resp, err := RequestAppRegistration(ctx, client, brand, errOut)
if err != nil {
    if strings.Contains(err.Error(), "missing device_code") {
        // server protocol problem: retry once, then report to maintainers
    }
    return err
}

Prevention

When it happens

Trigger: A 2xx JSON response from the registration begin endpoint lacking the device_code key — e.g. a protocol change on the server, a partially-implemented mock/stub server, a proxy rewriting the JSON, or a different service version responding at that URL.

Common situations: Hitting an environment where the accounts endpoint is served by an API gateway that returns an empty JSON object; running against a regional endpoint with an older/newer protocol; corporate middleboxes stripping response fields; server-side protocol regression.

Related errors


AI-assisted analysis of larksuite/cli@7fd6ef3c07 (2026-09-04). Data as JSON: /api/errors/77759cc06ef3a599. Report an issue: GitHub.