{"record":{"id":"e9fa5c63688eb9b3","repo":"jackwener/OpenCLI","slug":"zhihu-api-v4-me-returned-no-url-token-anonymous","errorCode":null,"errorMessage":"Zhihu /api/v4/me returned no url_token — anonymous session","messagePattern":"Zhihu /api/v4/me returned no url_token — anonymous session","errorType":"exception","errorClass":"AuthRequiredError","httpStatus":null,"severity":"error","filePath":"clis/zhihu/auth.js","lineNumber":37,"sourceCode":"        if (!r.ok) return { __httpError: r.status };\n        return await r.json();\n      } catch (e) {\n        return { __exception: String(e && e.message || e) };\n      }\n    })()\n  `);\n  if (data?.__exception) {\n    throw new CommandExecutionError(`Zhihu whoami failed: ${data.__exception}`);\n  }\n  if (!data || data.__httpError) {\n    const status = data?.__httpError;\n    if (status === 401 || status === 403) {\n      throw new AuthRequiredError('www.zhihu.com', `Zhihu /api/v4/me returned HTTP ${status} — anonymous`);\n    }\n    throw new CommandExecutionError(`Zhihu identity probe failed (HTTP ${status ?? 'unknown'})`);\n  }\n  if (!data.url_token) {\n    throw new AuthRequiredError('www.zhihu.com', 'Zhihu /api/v4/me returned no url_token — anonymous session');\n  }\n  return {\n    url_token: String(data.url_token),\n    name: String(data.name ?? ''),\n    uid: String(data.uid ?? data.id ?? ''),\n  };\n}\n\nregisterSiteAuthCommands({\n  site: 'zhihu',\n  domain: 'www.zhihu.com',\n  loginUrl: 'https://www.zhihu.com/signin',\n  columns: ['url_token', 'name', 'uid'],\n  quickCheck: hasZhihuAuthCookie,\n  verify: verifyZhihuIdentity,\n  poll: async (page) => {\n    if (!await hasZhihuAuthCookie(page)) {\n      throw new AuthRequiredError('www.zhihu.com', 'Waiting for Zhihu z_c0 cookie');","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/zhihu/auth.js#L19-L55","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Re-login with `opencli zhihu login` to establish a fully valid session, then retry.","Confirm in a normal browser that zhihu.com shows you as logged in with the same cookie profile.","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.","Catch AuthRequiredError and direct users to re-authentication instead of retrying."],"exampleFix":"// before\nconst me = await verifyZhihuIdentity(page); // AuthRequiredError: no url_token\n// after\ntry {\n  const me = await verifyZhihuIdentity(page);\n} catch (e) {\n  if (String(e.message).includes('no url_token')) {\n    await zhihuLogin(); // full re-authentication\n    return verifyZhihuIdentity(page);\n  }\n  throw e;\n}","handlingStrategy":"try-catch","validationCode":"const me = await fetchMeViaPage(page); // 2xx but check shape before proceeding\nif (!me || !me.url_token) await runZhihuLogin();","typeGuard":"function hasUrlToken(d) { return !!d && typeof d === 'object' && typeof d.url_token === 'string' && d.url_token.length > 0; }","tryCatchPattern":"try { const me = await verifyZhihuIdentity(page); } catch (e) { if (String(e.message).includes('no url_token')) { await zhihuLogin(); return verifyZhihuIdentity(page); } throw e; }","preventionTips":["After QR login, confirm the session is fully established before running commands","Keep the library updated in case Zhihu changes the /api/v4/me response shape","Verify login in a normal browser when url_token is missing","Catch this AuthRequiredError and route to re-authentication"],"tags":["authentication","zhihu","api-response","session"],"backgroundTag":"auth-session-invalid","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}