|
27 | 27 | import org.apache.http.client.config.RequestConfig; |
28 | 28 | import org.apache.http.client.methods.CloseableHttpResponse; |
29 | 29 | import org.apache.http.client.methods.HttpGet; |
| 30 | +import org.apache.http.client.utils.URIUtils; |
30 | 31 | import org.apache.http.impl.client.BasicCredentialsProvider; |
31 | 32 | import org.apache.http.impl.client.BasicResponseHandler; |
32 | 33 | import org.apache.http.impl.client.CloseableHttpClient; |
33 | 34 | import org.apache.http.impl.client.HttpClients; |
34 | 35 |
|
| 36 | +import javax.print.DocFlavor; |
35 | 37 | import java.io.File; |
36 | 38 | import java.io.IOException; |
37 | 39 | import java.io.InputStream; |
@@ -297,6 +299,98 @@ public WxMpSemanticQueryResult semanticQuery(WxMpSemanticQuery semanticQuery) th |
297 | 299 | return WxMpSemanticQueryResult.fromJson(responseContent); |
298 | 300 | } |
299 | 301 |
|
| 302 | + @Override |
| 303 | + public String oauth2buildAuthorizationUrl(String scope, String state) { |
| 304 | + String url = "https://open.weixin.qq.com/connect/oauth2/authorize?" ; |
| 305 | + url += "appid=" + wxMpConfigStorage.getAppId(); |
| 306 | + url += "&redirect_uri=" + URIUtil.encodeURIComponent(wxMpConfigStorage.getOauth2redirectUrl()); |
| 307 | + url += "&response_type=code"; |
| 308 | + url += "&scope=" + scope; |
| 309 | + if (state != null) { |
| 310 | + url += "&state=" + state; |
| 311 | + } |
| 312 | + url += "#wechat_redirect"; |
| 313 | + return url; |
| 314 | + } |
| 315 | + |
| 316 | + @Override |
| 317 | + public WxMpOAuth2AccessToken oauth2getAccessToken(String code) throws WxErrorException { |
| 318 | + String url = "https://api.weixin.qq.com/sns/oauth2/access_token?"; |
| 319 | + url += "appid=" + wxMpConfigStorage.getAppId(); |
| 320 | + url += "&secret=" + wxMpConfigStorage.getSecret(); |
| 321 | + url += "&code=" + code; |
| 322 | + url += "&grant_type=authorization_code"; |
| 323 | + |
| 324 | + try { |
| 325 | + RequestExecutor<String, String> executor = new SimpleGetRequestExecutor(); |
| 326 | + String responseText = executor.execute(getHttpclient(), httpProxy, url, null); |
| 327 | + return WxMpOAuth2AccessToken.fromJson(responseText); |
| 328 | + } catch (ClientProtocolException e) { |
| 329 | + throw new RuntimeException(e); |
| 330 | + } catch (IOException e) { |
| 331 | + throw new RuntimeException(e); |
| 332 | + } |
| 333 | + } |
| 334 | + |
| 335 | + @Override |
| 336 | + public WxMpOAuth2AccessToken oauth2refreshAccessToken(String refreshToken) throws WxErrorException { |
| 337 | + String url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?"; |
| 338 | + url += "appid=" + wxMpConfigStorage.getAppId(); |
| 339 | + url += "&grant_type=refresh_token"; |
| 340 | + url += "&refresh_token=" + refreshToken; |
| 341 | + |
| 342 | + try { |
| 343 | + RequestExecutor<String, String> executor = new SimpleGetRequestExecutor(); |
| 344 | + String responseText = executor.execute(getHttpclient(), httpProxy, url, null); |
| 345 | + return WxMpOAuth2AccessToken.fromJson(responseText); |
| 346 | + } catch (ClientProtocolException e) { |
| 347 | + throw new RuntimeException(e); |
| 348 | + } catch (IOException e) { |
| 349 | + throw new RuntimeException(e); |
| 350 | + } |
| 351 | + } |
| 352 | + |
| 353 | + @Override |
| 354 | + public WxMpUser oauth2getUserInfo(WxMpOAuth2AccessToken oAuth2AccessToken, String lang) throws WxErrorException { |
| 355 | + String url = "https://api.weixin.qq.com/sns/userinfo?"; |
| 356 | + url += "access_token=" + oAuth2AccessToken.getAccessToken(); |
| 357 | + url += "&openid=" + oAuth2AccessToken.getOpenId(); |
| 358 | + if (lang == null) { |
| 359 | + url += "&lang=zh_CN"; |
| 360 | + } else { |
| 361 | + url += "&lang=" + lang; |
| 362 | + } |
| 363 | + |
| 364 | + try { |
| 365 | + RequestExecutor<String, String> executor = new SimpleGetRequestExecutor(); |
| 366 | + String responseText = executor.execute(getHttpclient(), httpProxy, url, null); |
| 367 | + return WxMpUser.fromJson(responseText); |
| 368 | + } catch (ClientProtocolException e) { |
| 369 | + throw new RuntimeException(e); |
| 370 | + } catch (IOException e) { |
| 371 | + throw new RuntimeException(e); |
| 372 | + } |
| 373 | + } |
| 374 | + |
| 375 | + @Override |
| 376 | + public boolean oauth2validateAccessToken(WxMpOAuth2AccessToken oAuth2AccessToken) { |
| 377 | + String url = "https://api.weixin.qq.com/sns/auth?"; |
| 378 | + url += "access_token=" + oAuth2AccessToken; |
| 379 | + url += "&openid=" + oAuth2AccessToken.getOpenId(); |
| 380 | + |
| 381 | + try { |
| 382 | + RequestExecutor<String, String> executor = new SimpleGetRequestExecutor(); |
| 383 | + executor.execute(getHttpclient(), httpProxy, url, null); |
| 384 | + } catch (ClientProtocolException e) { |
| 385 | + throw new RuntimeException(e); |
| 386 | + } catch (IOException e) { |
| 387 | + throw new RuntimeException(e); |
| 388 | + } catch (WxErrorException e) { |
| 389 | + return false; |
| 390 | + } |
| 391 | + return true; |
| 392 | + } |
| 393 | + |
300 | 394 | public String get(String url, String queryParam) throws WxErrorException { |
301 | 395 | return execute(new SimpleGetRequestExecutor(), url, queryParam); |
302 | 396 | } |
|
0 commit comments