{"record":{"id":"63ba4d531bf50875","repo":"w7corp/easywechat","slug":"failed-to-get-jssdk-agentticket-s","errorCode":null,"errorMessage":"Failed to get jssdk agentTicket: %s","messagePattern":"Failed to get jssdk agentTicket: (.+?)","errorType":"exception","errorClass":"HttpException","httpStatus":null,"severity":"error","filePath":"src/OpenWork/JsApiTicket.php","lineNumber":118,"sourceCode":"        ];\n    }\n\n    /**\n     * @throws HttpException\n     */\n    public function getAgentTicket(int $agentId): string\n    {\n        $key = $this->getAgentKey($agentId);\n        $ticket = $this->cache->get($key);\n\n        if ($ticket && is_string($ticket)) {\n            return $ticket;\n        }\n\n        $response = $this->httpClient->request('GET', '/cgi-bin/ticket/get', ['query' => ['type' => 'agent_config']])->toArray(false);\n\n        if (empty($response['ticket'])) {\n            throw new HttpException('Failed to get jssdk agentTicket: '.json_encode($response, JSON_UNESCAPED_UNICODE));\n        }\n\n        $this->cache->set($key, $response['ticket'], intval($response['expires_in']));\n\n        return $response['ticket'];\n    }\n\n    public function getAgentKey(int $agentId): string\n    {\n        return sprintf('%s.%s', $this->getKey(), $agentId);\n    }\n}\n","sourceCodeStart":100,"sourceCodeEnd":131,"githubUrl":"https://github.com/w7corp/easywechat/blob/f0cf0a8b8361417ed683b8246d0ecbaf0aafcaa8/src/OpenWork/JsApiTicket.php#L100-L131","documentation":"getAgentTicket() returns the agent_config JS-SDK ticket used for third-party app agents (wx.agentConfig). It is cached per agentId (key '<base-key>.<agentId>'); on miss it GETs /cgi-bin/ticket/get?type=agent_config and throws HttpException with the raw JSON when no ticket comes back. Failures trace to the credential chain or the state of that specific agent.","triggerScenarios":"Running wx.agentConfig flows while the authorizer access token is invalid; the target agent (agentid) was deleted or the app version changed; suite credentials stale; a freshly reinstalled app whose tokens have not settled yet.","commonSituations":"Third-party WeChat Work apps signing agent-scoped JS-SDK after the authorizer changed or removed agents; stale per-agent cache entries surviving a reinstall; dev environments pointing at prod corp credentials.","solutions":["Decode the embedded errcode/errmsg first","Confirm the agentId still exists on the authorizer corp and matches the installed app version","Refresh authorizer/suite tokens, clear the per-agent ticket key, then retry","For freshly (re)installed apps, re-fetch after tokens settle instead of hammering retries"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"if ($agentId <= 0) {\n    throw new \\InvalidArgumentException('A valid numeric agentId is required for agent_config tickets.');\n}","typeGuard":null,"tryCatchPattern":"try {\n    $ticket = $jsApiTicket->getAgentTicket($agentId);\n} catch (\\EasyWeChat\\Kernel\\Exceptions\\HttpException $e) {\n    $payload = json_decode(strstr($e->getMessage(), '{') ?: '[]', true) ?: [];\n    // 40001-class → refresh authorizer/suite tokens then retry once; agent-missing errors → verify agentId\n    report($e);\n}","preventionTips":["Validate that the agentId still exists on the corp before signing agent-config payloads","Scope per-agent cache keys to the app version so reinstalled agents start clean","Retry with backoff for freshly installed apps instead of tight loops"],"tags":["php","easywechat","wechat-work","open-work","jsapi-ticket","agent-config","js-sdk"],"backgroundTag":"jsapi-ticket-fetch-failed","analyzedSha":"f0cf0a8b8361417ed683b8246d0ecbaf0aafcaa8","analyzedAt":"2026-08-21T05:29:19.565Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}