@@ -211,16 +211,22 @@ public String getPaidUnionId(String openid, String transactionId, String mchId,
211211
212212 @ Override
213213 public WxMaJscode2SessionResult jsCode2SessionInfo (String jsCode ) throws WxErrorException {
214- final WxMaConfig config = getWxMaConfig ();
215- Map <String , String > params = new HashMap <>(8 );
216- params .put ("appid" , config .getAppid ());
217- params .put ("secret" , config .getSecret ());
218- params .put ("js_code" , jsCode );
219- params .put ("grant_type" , "authorization_code" );
220-
221- String result =
222- get (JSCODE_TO_SESSION_URL , Joiner .on ("&" ).withKeyValueSeparator ("=" ).join (params ));
223- return WxMaJscode2SessionResult .fromJson (result );
214+ try {
215+ final WxMaConfig config = getWxMaConfig ();
216+ Map <String , String > params = new HashMap <>(8 );
217+ params .put ("appid" , SensitiveRequestUtils .encodeQueryValue (config .getAppid ()));
218+ params .put ("secret" , SensitiveRequestUtils .encodeQueryValue (config .getSecret ()));
219+ params .put ("js_code" , SensitiveRequestUtils .encodeQueryValue (jsCode ));
220+ params .put ("grant_type" , "authorization_code" );
221+
222+ String result =
223+ get (JSCODE_TO_SESSION_URL , Joiner .on ("&" ).withKeyValueSeparator ("=" ).join (params ));
224+ return WxMaJscode2SessionResult .fromJson (result );
225+ } catch (WxErrorException e ) {
226+ throw SensitiveRequestUtils .sanitize (e );
227+ } catch (RuntimeException e ) {
228+ throw SensitiveRequestUtils .sanitize (e );
229+ }
224230 }
225231
226232 @ Override
@@ -390,7 +396,7 @@ private <R, T> R executeWithRetry(ExecutorAction<R> executor, String uri, String
390396 int retryTimes = 0 ;
391397 do {
392398 try {
393- return this .executeInternal (executor , uri , dataForLog , false );
399+ return this .executeInternal (executor , uri , dataForLog , false , JSCODE_TO_SESSION_URL . equals ( uri ) );
394400 } catch (WxErrorException e ) {
395401 if (retryTimes + 1 > this .maxRetryTimes ) {
396402 log .warn ("重试达到最大次数【{}】" , maxRetryTimes );
@@ -423,7 +429,8 @@ private <R, T> R executeWithRetry(ExecutorAction<R> executor, String uri, String
423429 }
424430
425431 private <R , T > R executeInternal (
426- ExecutorAction <R > executor , String uri , String dataForLog , boolean doNotAutoRefreshToken )
432+ ExecutorAction <R > executor , String uri , String dataForLog , boolean doNotAutoRefreshToken ,
433+ boolean code2Session )
427434 throws WxErrorException {
428435
429436 if (uri .contains ("access_token=" )) {
@@ -440,7 +447,11 @@ private <R, T> R executeInternal(
440447 uri + (uri .contains ("?" ) ? "&" : "?" ) + "access_token=" + accessToken ;
441448 try {
442449 R result = executor .execute (uriWithAccessToken );
443- log .debug ("\n 【请求地址】: {}\n 【请求参数】:{}\n 【响应数据】:{}" , uriWithAccessToken , dataForLog , result );
450+ if (code2Session ) {
451+ log .debug ("code2Session request completed" );
452+ } else {
453+ log .debug ("\n 【请求地址】: {}\n 【请求参数】:{}\n 【响应数据】:{}" , uriWithAccessToken , dataForLog , result );
454+ }
444455 return result ;
445456 } catch (WxErrorException e ) {
446457 WxError error = e .getError ();
@@ -459,15 +470,18 @@ private <R, T> R executeInternal(
459470 }
460471 if (this .getWxMaConfig ().autoRefreshToken () && !doNotAutoRefreshToken ) {
461472 log .warn (
462- "即将重新获取新的access_token,错误代码:{},错误信息:{}" , error .getErrorCode (), error .getErrorMsg ());
473+ "即将重新获取新的access_token,错误代码:{},错误信息:{}" , error .getErrorCode (),
474+ code2Session ? "[redacted]" : error .getErrorMsg ());
463475 // 下一次不再自动重试
464476 // 当小程序误调用第三方平台专属接口时,第三方无法使用小程序的access token,如果可以继续自动获取token会导致无限循环重试,直到栈溢出
465- return this .executeInternal (executor , uri , dataForLog , true );
477+ return this .executeInternal (executor , uri , dataForLog , true , code2Session );
466478 }
467479 }
468480
469481 if (error .getErrorCode () != 0 ) {
470- if (error .getErrorCode () == WxMaErrorMsgEnum .CODE_43101 .getCode ()) {
482+ if (code2Session ) {
483+ log .warn ("code2Session request failed, error code: {}" , error .getErrorCode ());
484+ } else if (error .getErrorCode () == WxMaErrorMsgEnum .CODE_43101 .getCode ()) {
471485 // 43101 日志太多, 打印为debug, 其他情况打印为warn
472486 log .debug ("\n 【请求地址】: {}\n 【请求参数】:{}\n 【错误信息】:{}" , uriWithAccessToken , dataForLog , error );
473487 } else {
@@ -477,8 +491,12 @@ private <R, T> R executeInternal(
477491 }
478492 return null ;
479493 } catch (IOException e ) {
480- log .warn (
481- "\n 【请求地址】: {}\n 【请求参数】:{}\n 【异常信息】:{}" , uriWithAccessToken , dataForLog , e .getMessage ());
494+ if (code2Session ) {
495+ log .warn ("code2Session request failed, exception type: {}" , e .getClass ().getName ());
496+ } else {
497+ log .warn (
498+ "\n 【请求地址】: {}\n 【请求参数】:{}\n 【异常信息】:{}" , uriWithAccessToken , dataForLog , e .getMessage ());
499+ }
482500 throw new WxRuntimeException (e );
483501 }
484502 }
@@ -491,7 +509,7 @@ private <R, T> R executeInternal(
491509 * @throws WxErrorException 异常
492510 */
493511 protected String extractAccessToken (String resultContent ) throws WxErrorException {
494- log .debug ("access-token response: {}" , resultContent );
512+ log .debug ("access-token response received" );
495513 WxMaConfig config = this .getWxMaConfig ();
496514 WxError error = WxError .fromJson (resultContent , WxType .MiniApp );
497515 if (error .getErrorCode () != 0 ) {
0 commit comments