w7corp/easywechat · error · HttpException

Failed to get jssdk agentTicket

Error message

Failed to get jssdk agentTicket: %s

What it means

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.

Solutions

  1. Decode the embedded errcode/errmsg first
  2. Confirm the agentId still exists on the authorizer corp and matches the installed app version
  3. Refresh authorizer/suite tokens, clear the per-agent ticket key, then retry
  4. For freshly (re)installed apps, re-fetch after tokens settle instead of hammering retries
Defensive patterns

Strategy: try-catch

Validate before calling

if ($agentId <= 0) {
    throw new \InvalidArgumentException('A valid numeric agentId is required for agent_config tickets.');
}

Try / catch

try {
    $ticket = $jsApiTicket->getAgentTicket($agentId);
} catch (\EasyWeChat\Kernel\Exceptions\HttpException $e) {
    $payload = json_decode(strstr($e->getMessage(), '{') ?: '[]', true) ?: [];
    // 40001-class → refresh authorizer/suite tokens then retry once; agent-missing errors → verify agentId
    report($e);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of w7corp/easywechat@f0cf0a8b83 (2026-08-21). Data as JSON: /api/errors/63ba4d531bf50875. Report an issue: GitHub.

Appendix: source

Thrown at src/OpenWork/JsApiTicket.php:118

        ];
    }

    /**
     * @throws HttpException
     */
    public function getAgentTicket(int $agentId): string
    {
        $key = $this->getAgentKey($agentId);
        $ticket = $this->cache->get($key);

        if ($ticket && is_string($ticket)) {
            return $ticket;
        }

        $response = $this->httpClient->request('GET', '/cgi-bin/ticket/get', ['query' => ['type' => 'agent_config']])->toArray(false);

        if (empty($response['ticket'])) {
            throw new HttpException('Failed to get jssdk agentTicket: '.json_encode($response, JSON_UNESCAPED_UNICODE));
        }

        $this->cache->set($key, $response['ticket'], intval($response['expires_in']));

        return $response['ticket'];
    }

    public function getAgentKey(int $agentId): string
    {
        return sprintf('%s.%s', $this->getKey(), $agentId);
    }
}

View on GitHub (pinned to f0cf0a8b83)