jackwener/OpenCLI · error · AuthRequiredError

Zhihu /api/v4/me returned no url_token — anonymous session

Error message

Zhihu /api/v4/me returned no url_token — anonymous session

What it means

When /api/v4/me responds successfully (2xx) but the payload lacks url_token, the library considers the session effectively anonymous and throws AuthRequiredError. Zhihu normally includes url_token for authenticated users; its absence means the session cookie didn't grant a real identity.

Source

Thrown at clis/zhihu/auth.js:37

        if (!r.ok) return { __httpError: r.status };
        return await r.json();
      } catch (e) {
        return { __exception: String(e && e.message || e) };
      }
    })()
  `);
  if (data?.__exception) {
    throw new CommandExecutionError(`Zhihu whoami failed: ${data.__exception}`);
  }
  if (!data || data.__httpError) {
    const status = data?.__httpError;
    if (status === 401 || status === 403) {
      throw new AuthRequiredError('www.zhihu.com', `Zhihu /api/v4/me returned HTTP ${status} — anonymous`);
    }
    throw new CommandExecutionError(`Zhihu identity probe failed (HTTP ${status ?? 'unknown'})`);
  }
  if (!data.url_token) {
    throw new AuthRequiredError('www.zhihu.com', 'Zhihu /api/v4/me returned no url_token — anonymous session');
  }
  return {
    url_token: String(data.url_token),
    name: String(data.name ?? ''),
    uid: String(data.uid ?? data.id ?? ''),
  };
}

registerSiteAuthCommands({
  site: 'zhihu',
  domain: 'www.zhihu.com',
  loginUrl: 'https://www.zhihu.com/signin',
  columns: ['url_token', 'name', 'uid'],
  quickCheck: hasZhihuAuthCookie,
  verify: verifyZhihuIdentity,
  poll: async (page) => {
    if (!await hasZhihuAuthCookie(page)) {
      throw new AuthRequiredError('www.zhihu.com', 'Waiting for Zhihu z_c0 cookie');

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Re-login with `opencli zhihu login` to establish a fully valid session, then retry.
  2. Confirm in a normal browser that zhihu.com shows you as logged in with the same cookie profile.
  3. If Zhihu changed the API response shape, update the CLI/library to the latest version or file an issue, since url_token extraction may need adjustment.
  4. Catch AuthRequiredError and direct users to re-authentication instead of retrying.

Example fix

// before
const me = await verifyZhihuIdentity(page); // AuthRequiredError: no url_token
// after
try {
  const me = await verifyZhihuIdentity(page);
} catch (e) {
  if (String(e.message).includes('no url_token')) {
    await zhihuLogin(); // full re-authentication
    return verifyZhihuIdentity(page);
  }
  throw e;
}
Defensive patterns

Strategy: try-catch

Validate before calling

const me = await fetchMeViaPage(page); // 2xx but check shape before proceeding
if (!me || !me.url_token) await runZhihuLogin();

Type guard

function hasUrlToken(d) { return !!d && typeof d === 'object' && typeof d.url_token === 'string' && d.url_token.length > 0; }

Try / catch

try { const me = await verifyZhihuIdentity(page); } catch (e) { if (String(e.message).includes('no url_token')) { await zhihuLogin(); return verifyZhihuIdentity(page); } throw e; }

Prevention

When it happens

Trigger: A z_c0-like cookie passes the hasZhihuAuthCookie check but is invalid/limited, so the API returns 200 with an unauthenticated-shaped body containing no url_token.

Common situations: Partially valid session (cookie set but session not fully established after QR scan); Zhihu A/B or API shape change removing url_token from the response; cookie imported from another account/device being rejected server-side.

Related errors


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/e9fa5c63688eb9b3. Report an issue: GitHub.