@@ -70,6 +70,49 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
7070 return configStorage .getAccessToken ();
7171 }
7272
73+ @ Override
74+ public String getContactAccessToken (boolean forceRefresh ) throws WxErrorException {
75+ final WxCpConfigStorage configStorage = getWxCpConfigStorage ();
76+ if (!configStorage .isContactAccessTokenExpired () && !forceRefresh ) {
77+ return configStorage .getContactAccessToken ();
78+ }
79+ Lock lock = configStorage .getContactAccessTokenLock ();
80+ lock .lock ();
81+ try {
82+ // 拿到锁之后,再次判断一下最新的token是否过期,避免重刷
83+ if (!configStorage .isContactAccessTokenExpired () && !forceRefresh ) {
84+ return configStorage .getContactAccessToken ();
85+ }
86+ // 使用通讯录同步secret获取access_token
87+ String contactSecret = configStorage .getContactSecret ();
88+ if (contactSecret == null || contactSecret .trim ().isEmpty ()) {
89+ throw new WxErrorException ("通讯录同步secret未配置" );
90+ }
91+ String url = String .format (configStorage .getApiUrl (WxCpApiPathConsts .GET_TOKEN ),
92+ this .configStorage .getCorpId (), contactSecret );
93+ try {
94+ HttpGet httpGet = new HttpGet (url );
95+ if (getRequestHttpProxy () != null ) {
96+ RequestConfig config = RequestConfig .custom ().setProxy (getRequestHttpProxy ()).build ();
97+ httpGet .setConfig (config );
98+ }
99+ String resultContent = getRequestHttpClient ().execute (httpGet , ApacheBasicResponseHandler .INSTANCE );
100+ WxError error = WxError .fromJson (resultContent , WxType .CP );
101+ if (error .getErrorCode () != 0 ) {
102+ throw new WxErrorException (error );
103+ }
104+
105+ WxAccessToken accessToken = WxAccessToken .fromJson (resultContent );
106+ configStorage .updateContactAccessToken (accessToken .getAccessToken (), accessToken .getExpiresIn ());
107+ } catch (IOException e ) {
108+ throw new WxRuntimeException (e );
109+ }
110+ } finally {
111+ lock .unlock ();
112+ }
113+ return configStorage .getContactAccessToken ();
114+ }
115+
73116 @ Override
74117 public String getMsgAuditAccessToken (boolean forceRefresh ) throws WxErrorException {
75118 final WxCpConfigStorage configStorage = getWxCpConfigStorage ();
0 commit comments