justauth/JustAuth · error · AuthException

${cause.message}

Error message

${cause.message}

What it means

Generic AuthException wrapping any non-AlipayApiException thrown by alipayClient.execute while exchanging the authorization code for a token (grant_type=authorization_code). The message is the underlying cause's message; common culprits are NumberFormatException from Integer.parseInt(response.getExpiresIn()) on unexpected content, network errors, or the SDK throwing a RuntimeException.

Source

Thrown at src/main/java/me/zhyd/oauth/request/AuthAlipayRequest.java:158

    }

    @Override
    protected void checkCode(AuthCallback authCallback) {
        if (StringUtils.isEmpty(authCallback.getAuth_code())) {
            throw new AuthException(AuthResponseStatus.ILLEGAL_CODE, source);
        }
    }

    @Override
    public AuthToken getAccessToken(AuthCallback authCallback) {
        AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
        request.setGrantType("authorization_code");
        request.setCode(authCallback.getAuth_code());
        AlipaySystemOauthTokenResponse response;
        try {
            response = this.alipayClient.execute(request);
        } catch (Exception e) {
            throw new AuthException(e);
        }
        if (!response.isSuccess()) {
            throw new AuthException(response.getSubMsg());
        }
        return AuthToken.builder()
            .accessToken(response.getAccessToken())
            .uid(response.getUserId())
            .expireIn(Integer.parseInt(response.getExpiresIn()))
            .refreshToken(response.getRefreshToken())
            .build();
    }

    /**
     * 刷新access token (续期)
     *
     * @param authToken 登录成功后返回的Token信息
     * @return AuthResponse
     */

View on GitHub (pinned to 694bbf1b01)

Solutions

  1. Read the wrapped cause in the AuthException — its class and message identify the real failure
  2. If it is NumberFormatException, capture the raw response to see what the gateway actually returned (maintenance page, error JSON without expiresIn)
  3. Verify network/proxy configuration on the DefaultAlipayClient constructor
  4. Validate alipayPublicKey matches the app's encryption mode (public-key vs cert mode)
Defensive patterns

Strategy: try-catch

Try / catch

try {
    AuthToken t = alipayRequest.getAccessToken(callback);
} catch (AuthException e) {
    Throwable c = e.getCause() != null ? e.getCause() : e;
    if (c instanceof java.net.ConnectException || c instanceof java.net.SocketTimeoutException) {
        // transient network — safe to retry the code exchange ONLY if it never succeeded
        scheduleRetry();
    } else {
        log.error("alipay token exchange failed", c); throw e;
    }
}

Prevention

When it happens

Trigger: AuthAlipayRequest.getAccessToken where DefaultAlipayClient.execute throws an unchecked exception: malformed gateway response making getExpiresIn() non-numeric, connection reset, missing proxy. AlipayApiException is also caught here since the catch is on Exception.

Common situations: Alipay gateway returned an HTML error page or maintenance response so the parsed fields are garbage; proxy host/port misconfigured; alipayPublicKey wrong causing runtime errors during response verification.

Related errors


AI-assisted analysis of justauth/JustAuth@694bbf1b01 (2026-08-14). Data as JSON: /api/errors/251ca94f04cc0e6a. Report an issue: GitHub.