From 5c7448f212d556e677454f26c42dad99d546a16c Mon Sep 17 00:00:00 2001 From: Daniel Qian Date: Tue, 20 Jan 2015 14:30:44 +0800 Subject: [PATCH] =?UTF-8?q?=E9=92=88=E5=AF=B9AccessToken=EF=BC=8Cjsapi=20t?= =?UTF-8?q?icket=E6=B7=BB=E5=8A=A0=E5=BC=BA=E5=88=B6=E5=88=B7=E6=96=B0?= =?UTF-8?q?=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/me/chanjar/weixin/cp/api/WxCpService.java | 4 ++-- .../me/chanjar/weixin/cp/api/WxCpServiceImpl.java | 7 +++++-- .../java/me/chanjar/weixin/mp/api/WxMpService.java | 6 ++++-- .../me/chanjar/weixin/mp/api/WxMpServiceImpl.java | 14 ++++++++++---- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpService.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpService.java index c81bf715..36c78f0c 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpService.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpService.java @@ -51,11 +51,11 @@ public interface WxCpService { * 程序员在非必要情况下尽量不要主动调用此方法 * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=获取access_token * - * + * @param forceRefresh 强制刷新 * @return * @throws me.chanjar.weixin.common.exception.WxErrorException */ - public String getAccessToken() throws WxErrorException; + public String getAccessToken(boolean forceRefresh) throws WxErrorException; /** *
diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpServiceImpl.java
index c38ea65f..fb7255c6 100644
--- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpServiceImpl.java
+++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpServiceImpl.java
@@ -71,7 +71,10 @@ public class WxCpServiceImpl implements WxCpService {
     execute(new SimpleGetRequestExecutor(), url, null);
   }
 
-  public String getAccessToken() throws WxErrorException {
+  public String getAccessToken(boolean forceRefresh) throws WxErrorException {
+    if (forceRefresh) {
+      wxCpConfigStorage.expireAccessToken();
+    }
     if (wxCpConfigStorage.isAccessTokenExpired()) {
       synchronized (GLOBAL_ACCESS_TOKEN_REFRESH_LOCK) {
         if (wxCpConfigStorage.isAccessTokenExpired()) {
@@ -359,7 +362,7 @@ public class WxCpServiceImpl implements WxCpService {
    * @throws WxErrorException
    */
   public  T execute(RequestExecutor executor, String uri, E data) throws WxErrorException {
-    String accessToken = getAccessToken();
+    String accessToken = getAccessToken(false);
 
     String uriWithAccessToken = uri;
     uriWithAccessToken += uri.indexOf('?') == -1 ? "?access_token=" + accessToken : "&access_token=" + accessToken;
diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java
index cef912ee..5500376c 100644
--- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java
+++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java
@@ -40,10 +40,11 @@ public interface WxMpService {
 
    * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=获取access_token
    * 
+ * @param forceRefresh 强制刷新 * @return * @throws me.chanjar.weixin.common.exception.WxErrorException */ - public String getAccessToken() throws WxErrorException; + public String getAccessToken(boolean forceRefresh) throws WxErrorException; /** *
@@ -52,10 +53,11 @@ public interface WxMpService {
    *
    * 详情请见:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E9.99.84.E5.BD.951-JS-SDK.E4.BD.BF.E7.94.A8.E6.9D.83.E9.99.90.E7.AD.BE.E5.90.8D.E7.AE.97.E6.B3.95
    * 
+ * @param forceRefresh 强制刷新 * @return * @throws WxErrorException */ - public String getJsapiTicket() throws WxErrorException; + public String getJsapiTicket(boolean forceRefresh) throws WxErrorException; /** *
diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpServiceImpl.java
index 18d3bcdd..f1260f64 100644
--- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpServiceImpl.java
+++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpServiceImpl.java
@@ -69,7 +69,10 @@ public class WxMpServiceImpl implements WxMpService {
     }
   }
   
-  public String getAccessToken() throws WxErrorException {
+  public String getAccessToken(boolean forceRefresh) throws WxErrorException {
+    if (forceRefresh) {
+      wxMpConfigStorage.expireAccessToken();
+    }
     if (wxMpConfigStorage.isAccessTokenExpired()) {
       synchronized (GLOBAL_ACCESS_TOKEN_REFRESH_LOCK) {
         if (wxMpConfigStorage.isAccessTokenExpired()) {
@@ -104,7 +107,10 @@ public class WxMpServiceImpl implements WxMpService {
   }
 
 
-  public String getJsapiTicket() throws WxErrorException {
+  public String getJsapiTicket(boolean forceRefresh) throws WxErrorException {
+    if (forceRefresh) {
+      wxMpConfigStorage.expireJsapiTicket();
+    }
     if (wxMpConfigStorage.isJsapiTicketExpired()) {
       synchronized (GLOBAL_JSAPI_TICKET_REFRESH_LOCK) {
         if (wxMpConfigStorage.isJsapiTicketExpired()) {
@@ -122,7 +128,7 @@ public class WxMpServiceImpl implements WxMpService {
   }
 
   public String createJsapiSignature(String timestamp, String noncestr, String url) throws WxErrorException {
-    String jsapiTicket = getJsapiTicket();
+    String jsapiTicket = getJsapiTicket(false);
     try {
       return SHA1.genWithAmple(
           "jsapi_ticket=" + jsapiTicket,
@@ -436,7 +442,7 @@ public class WxMpServiceImpl implements WxMpService {
    * @throws WxErrorException
    */
   public  T execute(RequestExecutor executor, String uri, E data) throws WxErrorException {
-    String accessToken = getAccessToken();
+    String accessToken = getAccessToken(false);
     
     String uriWithAccessToken = uri;
     uriWithAccessToken += uri.indexOf('?') == -1 ? "?access_token=" + accessToken : "&access_token=" + accessToken;