@@ -56,12 +56,15 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
5656 this .configStorage .getCorpSecret ()))
5757 .get ()
5858 .build ();
59- String resultContent = null ;
60- try {
61- Response response = client .newCall (request ).execute ();
59+ String resultContent ;
60+ try (Response response = client .newCall (request ).execute ()) {
61+ if (response .body () == null ) {
62+ throw new WxErrorException ("请求access token失败:响应内容为空" );
63+ }
6264 resultContent = response .body ().string ();
6365 } catch (IOException e ) {
6466 log .error (e .getMessage (), e );
67+ throw new WxErrorException (e );
6568 }
6669
6770 WxError error = WxError .fromJson (resultContent , WxType .CP );
@@ -75,6 +78,55 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
7578 return this .configStorage .getAccessToken ();
7679 }
7780
81+ @ Override
82+ public String getContactAccessToken (boolean forceRefresh ) throws WxErrorException {
83+ if (!this .configStorage .isContactAccessTokenExpired () && !forceRefresh ) {
84+ return this .configStorage .getContactAccessToken ();
85+ }
86+
87+ Lock lock = this .configStorage .getContactAccessTokenLock ();
88+ lock .lock ();
89+ try {
90+ // 拿到锁之后,再次判断一下最新的token是否过期,避免重刷
91+ if (!this .configStorage .isContactAccessTokenExpired () && !forceRefresh ) {
92+ return this .configStorage .getContactAccessToken ();
93+ }
94+ // 使用通讯录同步secret获取access_token
95+ String contactSecret = this .configStorage .getContactSecret ();
96+ if (contactSecret == null || contactSecret .trim ().isEmpty ()) {
97+ throw new WxErrorException ("通讯录同步secret未配置" );
98+ }
99+ //得到httpClient
100+ OkHttpClient client = getRequestHttpClient ();
101+ //请求的request
102+ Request request = new Request .Builder ()
103+ .url (String .format (this .configStorage .getApiUrl (GET_TOKEN ), this .configStorage .getCorpId (),
104+ contactSecret ))
105+ .get ()
106+ .build ();
107+ String resultContent ;
108+ try (Response response = client .newCall (request ).execute ()) {
109+ if (response .body () == null ) {
110+ throw new WxErrorException ("请求通讯录同步access token失败:响应内容为空" );
111+ }
112+ resultContent = response .body ().string ();
113+ } catch (IOException e ) {
114+ log .error (e .getMessage (), e );
115+ throw new WxErrorException (e );
116+ }
117+ WxError error = WxError .fromJson (resultContent , WxType .CP );
118+ if (error .getErrorCode () != 0 ) {
119+ throw new WxErrorException (error );
120+ }
121+ WxAccessToken accessToken = WxAccessToken .fromJson (resultContent );
122+ this .configStorage .updateContactAccessToken (accessToken .getAccessToken (),
123+ accessToken .getExpiresIn ());
124+ } finally {
125+ lock .unlock ();
126+ }
127+ return this .configStorage .getContactAccessToken ();
128+ }
129+
78130 @ Override
79131 public String getMsgAuditAccessToken (boolean forceRefresh ) throws WxErrorException {
80132 if (!this .configStorage .isMsgAuditAccessTokenExpired () && !forceRefresh ) {
@@ -101,11 +153,15 @@ public String getMsgAuditAccessToken(boolean forceRefresh) throws WxErrorExcepti
101153 msgAuditSecret ))
102154 .get ()
103155 .build ();
104- String resultContent = null ;
156+ String resultContent ;
105157 try (Response response = client .newCall (request ).execute ()) {
158+ if (response .body () == null ) {
159+ throw new WxErrorException ("请求会话存档access token失败:响应内容为空" );
160+ }
106161 resultContent = response .body ().string ();
107162 } catch (IOException e ) {
108163 log .error (e .getMessage (), e );
164+ throw new WxErrorException (e );
109165 }
110166
111167 WxError error = WxError .fromJson (resultContent , WxType .CP );
0 commit comments