mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-10-31 10:38:42 +08:00 
			
		
		
		
	#639 修复小程序代码模版库管理 access_token key 错误
This commit is contained in:
		| @ -115,11 +115,15 @@ public class WxOpenComponentServiceImpl implements WxOpenComponentService { | ||||
|   } | ||||
|  | ||||
|   private String post(String uri, String postData) throws WxErrorException { | ||||
|     return post(uri, postData, "component_access_token"); | ||||
|   } | ||||
|  | ||||
|   private String post(String uri, String postData, String accessTokenKey) throws WxErrorException { | ||||
|     String componentAccessToken = getComponentAccessToken(false); | ||||
|     String uriWithComponentAccessToken = uri + (uri.contains("?") ? "&" : "?") + "component_access_token=" + componentAccessToken; | ||||
|     String uriWithComponentAccessToken = uri + (uri.contains("?") ? "&" : "?") + accessTokenKey + "=" + componentAccessToken; | ||||
|     try { | ||||
|       return getWxOpenService().post(uriWithComponentAccessToken, postData); | ||||
|     }catch (WxErrorException e){ | ||||
|     } catch (WxErrorException e) { | ||||
|       WxError error = e.getError(); | ||||
|       /* | ||||
|        * 发生以下情况时尝试刷新access_token | ||||
| @ -131,7 +135,7 @@ public class WxOpenComponentServiceImpl implements WxOpenComponentService { | ||||
|         // 强制设置wxMpConfigStorage它的access token过期了,这样在下一次请求里就会刷新access token | ||||
|         this.getWxOpenConfigStorage().expireComponentAccessToken(); | ||||
|         if (this.getWxOpenConfigStorage().autoRefreshToken()) { | ||||
|           return this.post(uri, postData); | ||||
|           return this.post(uri, postData, accessTokenKey); | ||||
|         } | ||||
|       } | ||||
|       if (error.getErrorCode() != 0) { | ||||
| @ -142,11 +146,14 @@ public class WxOpenComponentServiceImpl implements WxOpenComponentService { | ||||
|   } | ||||
|  | ||||
|   private String get(String uri) throws WxErrorException { | ||||
|     return get(uri, "component_access_token"); | ||||
|   } | ||||
|   private String get(String uri, String accessTokenKey) throws WxErrorException { | ||||
|     String componentAccessToken = getComponentAccessToken(false); | ||||
|     String uriWithComponentAccessToken = uri + (uri.contains("?") ? "&" : "?") + "component_access_token=" + componentAccessToken; | ||||
|     String uriWithComponentAccessToken = uri + (uri.contains("?") ? "&" : "?") + accessTokenKey + "=" + componentAccessToken; | ||||
|     try { | ||||
|       return getWxOpenService().get(uriWithComponentAccessToken, null); | ||||
|     }catch (WxErrorException e){ | ||||
|     } catch (WxErrorException e) { | ||||
|       WxError error = e.getError(); | ||||
|       /* | ||||
|        * 发生以下情况时尝试刷新access_token | ||||
| @ -158,7 +165,7 @@ public class WxOpenComponentServiceImpl implements WxOpenComponentService { | ||||
|         // 强制设置wxMpConfigStorage它的access token过期了,这样在下一次请求里就会刷新access token | ||||
|         this.getWxOpenConfigStorage().expireComponentAccessToken(); | ||||
|         if (this.getWxOpenConfigStorage().autoRefreshToken()) { | ||||
|           return this.get(uri); | ||||
|           return this.get(uri, accessTokenKey); | ||||
|         } | ||||
|       } | ||||
|       if (error.getErrorCode() != 0) { | ||||
| @ -298,7 +305,7 @@ public class WxOpenComponentServiceImpl implements WxOpenComponentService { | ||||
|  | ||||
|   @Override | ||||
|   public List<WxOpenMaCodeTemplate> getTemplateDraftList() throws WxErrorException { | ||||
|     String responseContent = get(GET_TEMPLATE_DRAFT_LIST_URL); | ||||
|     String responseContent = get(GET_TEMPLATE_DRAFT_LIST_URL, "access_token"); | ||||
|     JsonObject response = JSON_PARSER.parse(StringUtils.defaultString(responseContent, "{}")).getAsJsonObject(); | ||||
|     boolean hasDraftList = response.has("draft_list"); | ||||
|     if (hasDraftList) { | ||||
| @ -312,7 +319,7 @@ public class WxOpenComponentServiceImpl implements WxOpenComponentService { | ||||
|  | ||||
|   @Override | ||||
|   public List<WxOpenMaCodeTemplate> getTemplateList() throws WxErrorException { | ||||
|     String responseContent = get(GET_TEMPLATE_LIST_URL); | ||||
|     String responseContent = get(GET_TEMPLATE_LIST_URL, "access_token"); | ||||
|     JsonObject response = JSON_PARSER.parse(StringUtils.defaultString(responseContent, "{}")).getAsJsonObject(); | ||||
|     boolean hasDraftList = response.has("template_list"); | ||||
|     if (hasDraftList) { | ||||
| @ -328,13 +335,13 @@ public class WxOpenComponentServiceImpl implements WxOpenComponentService { | ||||
|   public void addToTemplate(long draftId) throws WxErrorException { | ||||
|     JsonObject param = new JsonObject(); | ||||
|     param.addProperty("draft_id", draftId); | ||||
|     post(ADD_TO_TEMPLATE_URL, param.toString()); | ||||
|     post(ADD_TO_TEMPLATE_URL, param.toString(), "access_token"); | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public void deleteTemplate(long templateId) throws WxErrorException { | ||||
|     JsonObject param = new JsonObject(); | ||||
|     param.addProperty("template_id", templateId); | ||||
|     post(DELETE_TEMPLATE_URL, param.toString()); | ||||
|     post(DELETE_TEMPLATE_URL, param.toString(), "access_token"); | ||||
|   } | ||||
| } | ||||
|  | ||||
| @ -14,19 +14,12 @@ import me.chanjar.weixin.open.api.WxOpenComponentService; | ||||
|   private WxOpenComponentService wxOpenComponentService; | ||||
|   private WxMaConfig wxMaConfig; | ||||
|   private String appId; | ||||
|   private WxMaUserService wxMaUserService; | ||||
|  | ||||
|   public WxOpenMaServiceImpl(WxOpenComponentService wxOpenComponentService, String appId, WxMaConfig wxMaConfig) { | ||||
|     this.wxOpenComponentService = wxOpenComponentService; | ||||
|     this.appId = appId; | ||||
|     this.wxMaConfig = wxMaConfig; | ||||
|     initHttp(); | ||||
|     this.wxMaUserService = new WxOpenMaUserServiceImpl(wxOpenComponentService, this); | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public WxMaUserService getUserService() { | ||||
|     return this.wxMaUserService; | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|  | ||||
| @ -1,70 +0,0 @@ | ||||
| package me.chanjar.weixin.open.api.impl; | ||||
|  | ||||
| import cn.binarywang.wx.miniapp.api.WxMaService; | ||||
| import cn.binarywang.wx.miniapp.api.WxMaUserService; | ||||
| import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; | ||||
| import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo; | ||||
| import cn.binarywang.wx.miniapp.bean.WxMaUserInfo; | ||||
| import cn.binarywang.wx.miniapp.util.crypt.WxMaCryptUtils; | ||||
| import com.google.common.base.Joiner; | ||||
| import me.chanjar.weixin.common.error.WxErrorException; | ||||
| import me.chanjar.weixin.open.api.WxOpenComponentService; | ||||
| import org.apache.commons.codec.digest.DigestUtils; | ||||
|  | ||||
| import java.util.HashMap; | ||||
| import java.util.Map; | ||||
|  | ||||
| /** | ||||
|  * @author Charming | ||||
|  */ | ||||
| class WxOpenMaUserServiceImpl implements WxMaUserService { | ||||
|   private static final String COMPONENT_JSCODE_TO_SESSION_URL = "https://api.weixin.qq.com/sns/component/jscode2session"; | ||||
|   private WxOpenComponentService wxOpenComponentService; | ||||
|   private WxMaService wxMaService; | ||||
|  | ||||
|   public WxOpenMaUserServiceImpl(WxOpenComponentService wxOpenComponentService, WxMaService wxMaService) { | ||||
|     this.wxOpenComponentService = wxOpenComponentService; | ||||
|     this.wxMaService = wxMaService; | ||||
|   } | ||||
|  | ||||
|   /** | ||||
|    * 第三方平台开发者的服务器使用登录凭证 code 以及 | ||||
|    * 第三方平台的 component_access_token | ||||
|    * 获取 session_key 和 openid。 | ||||
|    * 其中 session_key 是对用户数据进行加密签名的密钥。 | ||||
|    * 为了自身应用安全,session_key 不应该在网络上传输。 | ||||
|    * 文档:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1492585163_FtTNA&token=&lang=zh_CN | ||||
|    * | ||||
|    * @param jsCode 登录时获取的 code | ||||
|    * @return session_key 和 openid | ||||
|    * @throws WxErrorException 发生错误时 | ||||
|    */ | ||||
|   @Override | ||||
|   public WxMaJscode2SessionResult getSessionInfo(String jsCode)  throws WxErrorException { | ||||
|     Map<String, String> params = new HashMap<>(5); | ||||
|     params.put("appid", wxMaService.getWxMaConfig().getAppid()); | ||||
|     params.put("js_code", jsCode); | ||||
|     params.put("grant_type", "authorization_code"); | ||||
|     params.put("component_appid", wxOpenComponentService.getWxOpenConfigStorage().getComponentAppId()); | ||||
|     params.put("component_access_token", wxOpenComponentService.getComponentAccessToken(false)); | ||||
|  | ||||
|     String result = this.wxMaService.get(COMPONENT_JSCODE_TO_SESSION_URL, Joiner.on("&").withKeyValueSeparator("=").join(params)); | ||||
|     return WxMaJscode2SessionResult.fromJson(result); | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public WxMaUserInfo getUserInfo(String sessionKey, String encryptedData, String ivStr) { | ||||
|     return WxMaUserInfo.fromJson(WxMaCryptUtils.decrypt(sessionKey, encryptedData, ivStr)); | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public WxMaPhoneNumberInfo getPhoneNoInfo(String sessionKey, String encryptedData, String ivStr) { | ||||
|     return WxMaPhoneNumberInfo.fromJson(WxMaCryptUtils.decrypt(sessionKey, encryptedData, ivStr)); | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public boolean checkUserInfo(String sessionKey, String rawData, String signature) { | ||||
|     final String generatedSignature = DigestUtils.sha1Hex(rawData + sessionKey); | ||||
|     return generatedSignature.equals(signature); | ||||
|   } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 007gzs
					007gzs