component

This commit is contained in:
胡金宇 2017-08-14 13:28:04 +08:00
parent af879a9d32
commit 17949abed2
4 changed files with 2247 additions and 2254 deletions

View File

@ -57,8 +57,7 @@ public class WeixinComponentProxy {
* token管理
*/
public WeixinComponentProxy(CacheStorager<Token> cacheStorager) {
this(JSON.parseObject(Weixin4jConfigUtil.getValue("account"),
WeixinMpAccount.class), cacheStorager);
this(JSON.parseObject(Weixin4jConfigUtil.getValue("account"), WeixinMpAccount.class), cacheStorager);
}
/**
@ -69,26 +68,20 @@ public class WeixinComponentProxy {
* @param cacheStorager
* token管理
*/
public WeixinComponentProxy(WeixinMpAccount weixinMpAccount,
CacheStorager<Token> cacheStorager) {
public WeixinComponentProxy(WeixinMpAccount weixinMpAccount, CacheStorager<Token> cacheStorager) {
if (weixinMpAccount == null) {
throw new IllegalArgumentException(
"weixinMpAccount must not be empty");
throw new IllegalArgumentException("weixinMpAccount must not be empty");
}
if (cacheStorager == null) {
throw new IllegalArgumentException(
"cacheStorager must not be empty");
throw new IllegalArgumentException("cacheStorager must not be empty");
}
this.weixinMpAccount = weixinMpAccount;
this.componentMap = new HashMap<String, ComponentApi>(weixinMpAccount
.getComponents().size());
this.componentMap = new HashMap<String, ComponentApi>(weixinMpAccount.getComponents().size());
for (WeixinAccount component : weixinMpAccount.getComponents()) {
this.componentMap.put(component.getId(), new ComponentApi(
new TicketManager(component.getId(), component.getSecret(),
cacheStorager)));
this.componentMap.put(component.getId(),
new ComponentApi(new TicketManager(component.getId(), component.getSecret(), cacheStorager)));
}
this.componentMap.put(null, componentMap.get(weixinMpAccount
.getComponents().get(0).getId()));
this.componentMap.put(null, componentMap.get(weixinMpAccount.getComponents().get(0).getId()));
}
/**
@ -134,8 +127,7 @@ public class WeixinComponentProxy {
* @see com.foxinmy.weixin4j.mp.api.ComponentApi#getPreCodeManager()
* @throws WeixinException
*/
public String getPreComponentTicket(String componentId)
throws WeixinException {
public String getPreComponentTicket(String componentId) throws WeixinException {
ComponentApi component = component(componentId);
Token token = component.getTicketManager().getTicket();
if (token == null || StringUtil.isBlank(token.getAccessToken())) {
@ -153,16 +145,14 @@ public class WeixinComponentProxy {
* 组件ticket内容
* @throws WeixinException
*/
public void cacheComponentTicket(String componentId, String componentTicket)
throws WeixinException {
component(componentId).getTicketManager()
.cachingTicket(componentTicket);
public void cacheComponentTicket(String componentId, String componentTicket) throws WeixinException {
component(componentId).getTicketManager().cachingTicket(componentTicket);
}
/**
* 应用组件授权 <font color="red">需先缓存ticket</font> <li>
* redirectUri默认填写weixin4j.properties#component.oauth.redirect.uri <li>
* state默认填写state
* 应用组件授权 <font color="red">需先缓存ticket</font>
* <li>redirectUri默认填写weixin4j.properties#component.oauth.redirect.uri
* <li>state默认填写state
*
* @param componentId
* 组件ID
@ -170,17 +160,14 @@ public class WeixinComponentProxy {
* @return 请求授权的URL
* @throws WeixinException
*/
public String getComponentAuthorizationURL(String componentId)
throws WeixinException {
String redirectUri = Weixin4jConfigUtil
.getValue("component.oauth.redirect.uri");
public String getComponentAuthorizationURL(String componentId) throws WeixinException {
String redirectUri = Weixin4jConfigUtil.getValue("component.oauth.redirect.uri");
return getComponentAuthorizationURL(componentId, redirectUri, "state");
}
/**
* 应用组件授权 <font
* color="red">需先缓存ticket在授权完成之后需要调用ComponentApi#exchangeAuthInfo方法
* ,否则无法缓存token相关导致后续的组件接口调用失败</font>
* 应用组件授权 <font color="red">需先缓存ticket在授权完成之后需要调用ComponentApi#
* exchangeAuthorizerToken方法 ,否则无法缓存token相关导致后续的组件接口调用失败</font>
*
* @param componentId
* 组件ID
@ -192,17 +179,17 @@ public class WeixinComponentProxy {
* @see com.foxinmy.weixin4j.mp.api.ComponentApi
* @see com.foxinmy.weixin4j.mp.api.ComponentApi#getTicketManager()
* @see com.foxinmy.weixin4j.mp.api.ComponentApi#getPreCodeManager()
* @see com.foxinmy.weixin4j.mp.api.ComponentApi#exchangeAuthInfo(String)
* @see <a
* href="https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1453779503&token=&lang=zh_CN">应用组件授权</a>
* @see com.foxinmy.weixin4j.mp.api.ComponentApi#exchangeAuthorizerToken(String)
* @see <a href=
* "https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1453779503&token=&lang=zh_CN">
* 应用组件授权</a>
* @return 请求授权的URL
* @throws WeixinException
*/
public String getComponentAuthorizationURL(String componentId,
String redirectUri, String state) throws WeixinException {
public String getComponentAuthorizationURL(String componentId, String redirectUri, String state)
throws WeixinException {
try {
return String.format(URLConsts.COMPONENT_OAUTH_URL, componentId,
getPreComponentTicket(componentId),
return String.format(URLConsts.COMPONENT_OAUTH_URL, componentId, getPreComponentTicket(componentId),
URLEncoder.encode(redirectUri, Consts.UTF_8.name()), state);
} catch (UnsupportedEncodingException e) {
;
@ -210,5 +197,20 @@ public class WeixinComponentProxy {
return "";
}
/**
* 创建WeixinProxy对象
*
* @param componentId
* 组件ID
* @param authAppId
* 已授权的appid
* @see com.foxinmy.weixin4j.mp.WeixinProxy
* @return
*/
public WeixinProxy getWeixinProxy(String componentId, String authAppId) {
return new WeixinProxy(component(componentId).getRefreshTokenManager(authAppId),
component(componentId).getTokenManager());
}
public final static String VERSION = "1.7.7";
}

View File

@ -188,6 +188,23 @@ public class WeixinProxy {
this(weixinAccount, new WeixinTokenCreator(weixinAccount.getId(), weixinAccount.getSecret()), cacheStorager);
}
/**
* 第三方组件方式创建微信接口实现(永久刷新令牌机制)
*
* @param perTicketManager
* 第三方组件永久刷新token
* @param componentTokenManager
* 第三方组件凭证token
* @see com.foxinmy.weixin4j.mp.api.ComponentApi
* @see com.foxinmy.weixin4j.mp.api.ComponentApi#getPerCodeManager(String)
* @see com.foxinmy.weixin4j.mp.api.ComponentApi#getTokenManager
*/
public WeixinProxy(PerTicketManager perTicketManager, TokenManager componentTokenManager) {
this(new WeixinAccount(perTicketManager.getThirdId(), perTicketManager.getThirdSecret()),
new WeixinTokenComponentCreator(perTicketManager, componentTokenManager),
perTicketManager.getCacheStorager());
}
/**
* 微信接口实现
*
@ -264,8 +281,7 @@ public class WeixinProxy {
* @return
*/
public TokenManager getTicketManager(TicketType ticketType) {
return new TokenManager(new WeixinTicketCreator(ticketType, this.tokenManager),
this.cacheStorager);
return new TokenManager(new WeixinTicketCreator(ticketType, this.tokenManager), this.cacheStorager);
}
/**
@ -860,7 +876,8 @@ public class WeixinProxy {
* @throws WeixinException
* @see com.foxinmy.weixin4j.mp.api.MassApi
* @see <a href=
* "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140549&token=&lang=zh_CN">根据标签群发</a>
* "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140549&token=&lang=zh_CN">
* 根据标签群发</a>
*/
public String[] massToAll(MassTuple tuple) throws WeixinException {
return massApi.massToAll(tuple);
@ -879,7 +896,8 @@ public class WeixinProxy {
* @see {@link TagApi#listTags()}
* @see com.foxinmy.weixin4j.mp.api.MassApi
* @see <a href=
* "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140549&token=&lang=zh_CN">根据标签群发</a>
* "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140549&token=&lang=zh_CN">
* 根据标签群发</a>
*/
public String[] massByTagId(MassTuple tuple, int tagId) throws WeixinException {
return massApi.massByTagId(tuple, tagId);
@ -896,7 +914,8 @@ public class WeixinProxy {
* 图文消息被判定为转载时是否继续群发
* @return 第一个元素为消息发送任务的ID,第二个元素为消息的数据ID该字段只有在群发图文消息时才会出现
* @see <a href=
* "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140549&token=&lang=zh_CN">根据标签群发</a>
* "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140549&token=&lang=zh_CN">
* 根据标签群发</a>
* @see {@link #massByTagId(Tuple,int)}
* @see com.foxinmy.weixin4j.tuple.MpArticle
* @see com.foxinmy.weixin4j.mp.api.MassApi
@ -917,7 +936,8 @@ public class WeixinProxy {
* @return 第一个元素为消息发送任务的ID,第二个元素为消息的数据ID该字段只有在群发图文消息时才会出现,可以用于在图文分析数据接口中
* @throws WeixinException
* @see <a href=
* "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140549&token=&lang=zh_CN">根据openid群发</a>
* "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140549&token=&lang=zh_CN">
* 根据openid群发</a>
* @see {@link UserApi#getUser(String)}
* @see com.foxinmy.weixin4j.mp.api.MassApi
*/
@ -936,7 +956,8 @@ public class WeixinProxy {
* openId列表
* @return 第一个元素为消息发送任务的ID,第二个元素为消息的数据ID该字段只有在群发图文消息时才会出现,可以用于在图文分析数据接口中.
* @see <a href=
* "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140549&token=&lang=zh_CN">根据openid群发</a>
* "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140549&token=&lang=zh_CN">
* 根据openid群发</a>
* @see {@link #massByOpenIds(Tuple,String...)}
* @see com.foxinmy.weixin4j.tuple.MpArticle
* @see com.foxinmy.weixin4j.mp.api.MassApi
@ -954,7 +975,8 @@ public class WeixinProxy {
* 发送出去的消息ID
* @throws WeixinException
* @see <a href=
* "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140549&token=&lang=zh_CN">删除群发</a>
* "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140549&token=&lang=zh_CN">
* 删除群发</a>
* @see #deleteMassNews(String, int)
* @see com.foxinmy.weixin4j.mp.api.MassApi
*/
@ -974,7 +996,8 @@ public class WeixinProxy {
* 要删除的文章在图文消息中的位置第一篇编号为1该字段不填或填0会删除全部文章
* @throws WeixinException
* @see <a href=
* "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140549&token=&lang=zh_CN">删除群发</a>
* "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140549&token=&lang=zh_CN">
* 删除群发</a>
* @see {@link #massByTagId(Tuple, int)}
* @see {@link #massByOpenIds(Tuple, String...)
* @see com.foxinmy.weixin4j.mp.api.MassApi
@ -1591,7 +1614,8 @@ public class WeixinProxy {
* out of limit" }错误返回码。
*
* @see <a href=
* "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433744592&token=&lang=zh_CN">接口清零</a>
* "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433744592&token=&lang=zh_CN">
* 接口清零</a>
* @see com.foxinmy.weixin4j.mp.api.HelperApi
* @return 操作结果
* @throws WeixinException
@ -1729,7 +1753,8 @@ public class WeixinProxy {
* @see com.foxinmy.weixin4j.mp.api.TagApi
* @see com.foxinmy.weixin4j.mp.model.Tag
* @see <a href=
* "http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837&token=&lang=zh_CN">创建标签</a>
* "http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837&token=&lang=zh_CN">
* 创建标签</a>
*/
public Tag createTag(String name) throws WeixinException {
return tagApi.createTag(name);
@ -1743,7 +1768,8 @@ public class WeixinProxy {
* @see com.foxinmy.weixin4j.mp.api.TagApi
* @see com.foxinmy.weixin4j.mp.model.Tag
* @see <a href=
* "http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837&token=&lang=zh_CN">获取标签</a>
* "http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837&token=&lang=zh_CN">
* 获取标签</a>
*/
public List<Tag> listTags() throws WeixinException {
return tagApi.listTags();
@ -1759,7 +1785,8 @@ public class WeixinProxy {
* @see com.foxinmy.weixin4j.mp.api.TagApi
* @see com.foxinmy.weixin4j.mp.model.Tag
* @see <a href=
* "http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837&token=&lang=zh_CN">更新标签</a>
* "http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837&token=&lang=zh_CN">
* 更新标签</a>
*/
public ApiResult updateTag(Tag tag) throws WeixinException {
return tagApi.updateTag(tag);
@ -1774,7 +1801,8 @@ public class WeixinProxy {
* @see com.foxinmy.weixin4j.mp.api.TagApi
* @throws WeixinException
* @see <a href=
* "http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837&token=&lang=zh_CN">删除标签</a>
* "http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837&token=&lang=zh_CN">
* 删除标签</a>
*/
public ApiResult deleteTag(int tagId) throws WeixinException {
return tagApi.deleteTag(tagId);
@ -1791,7 +1819,8 @@ public class WeixinProxy {
* @throws WeixinException
* @see com.foxinmy.weixin4j.mp.api.TagApi
* @see <a href=
* "http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837&token=&lang=zh_CN">批量为用户打标签</a>
* "http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837&token=&lang=zh_CN">
* 批量为用户打标签</a>
*/
public ApiResult taggingUsers(int tagId, String... openIds) throws WeixinException {
return tagApi.taggingUsers(tagId, openIds);
@ -1808,7 +1837,8 @@ public class WeixinProxy {
* @throws WeixinException
* @see com.foxinmy.weixin4j.mp.api.TagApi
* @see <a href=
* "http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837&token=&lang=zh_CN">批量为用户取消标签</a>
* "http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837&token=&lang=zh_CN">
* 批量为用户取消标签</a>
*/
public ApiResult untaggingUsers(int tagId, String... openIds) throws WeixinException {
return tagApi.untaggingUsers(tagId, openIds);
@ -1825,7 +1855,8 @@ public class WeixinProxy {
* @throws WeixinException
* @see com.foxinmy.weixin4j.mp.api.TagApi
* @see <a href=
* "http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837&token=&lang=zh_CN">获取标签下粉丝列表</a>
* "http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837&token=&lang=zh_CN">
* 获取标签下粉丝列表</a>
*/
public Following getTagFollowingOpenIds(int tagId, String nextOpenId) throws WeixinException {
return tagApi.getTagFollowingOpenIds(tagId, nextOpenId);
@ -1842,7 +1873,8 @@ public class WeixinProxy {
* @throws WeixinException
* @see com.foxinmy.weixin4j.mp.api.TagApi
* @see <a href=
* "http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837&token=&lang=zh_CN">获取标签下粉丝列表</a>
* "http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837&token=&lang=zh_CN">
* 获取标签下粉丝列表</a>
*/
public Following getTagFollowing(int tagId, String nextOpenId) throws WeixinException {
return tagApi.getTagFollowing(tagId, nextOpenId);
@ -1858,7 +1890,8 @@ public class WeixinProxy {
* @see com.foxinmy.weixin4j.mp.api.TagApi
* @see #getTagFollowingOpenIds(int,String)
* @see <a href=
* "http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837&token=&lang=zh_CN">获取标签下粉丝列表</a>
* "http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837&token=&lang=zh_CN">
* 获取标签下粉丝列表</a>
*/
public List<String> getAllTagFollowingOpenIds(int tagId) throws WeixinException {
return tagApi.getAllTagFollowingOpenIds(tagId);
@ -1874,7 +1907,8 @@ public class WeixinProxy {
* @see com.foxinmy.weixin4j.mp.api.TagApi
* @see #getTagFollowing(int,String)
* @see <a href=
* "http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837&token=&lang=zh_CN">获取标签下粉丝列表</a>
* "http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837&token=&lang=zh_CN">
* 获取标签下粉丝列表</a>
*/
public List<User> getAllTagFollowing(int tagId) throws WeixinException {
return tagApi.getAllTagFollowing(tagId);
@ -1942,7 +1976,8 @@ public class WeixinProxy {
* @return 操作结果
* @see com.foxinmy.weixin4j.mp.api.TagApi
* @see <a href=
* "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1471422259_pJMWA&token=&lang=zh_CN">黑名单操作</a>
* "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1471422259_pJMWA&token=&lang=zh_CN">
* 黑名单操作</a>
* @throws WeixinException
*/
public ApiResult batchBlacklist(boolean blacklist, String... openIds) throws WeixinException {
@ -1962,7 +1997,8 @@ public class WeixinProxy {
* @param cardCoupon
* 卡券对象
* @see <a href=
* "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1451025056&token=&lang=zh_CN">创建卡券</a>
* "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1451025056&token=&lang=zh_CN">
* 创建卡券</a>
* @see CardCoupons
* @see MediaApi#uploadImage(java.io.InputStream, String)
* @see com.foxinmy.weixin4j.mp.api.CardApi
@ -2019,7 +2055,8 @@ public class WeixinProxy {
* @see com.foxinmy.weixin4j.model.qr.QRParameter
* @see com.foxinmy.weixin4j.mp.api.CardApi
* @see <a href=
* "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1451025062&token=&lang=zh_CN">投放卡券</a>
* "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1451025062&token=&lang=zh_CN">
* 投放卡券</a>
* @throws WeixinException
*/
public QRResult createCardQR(Integer expireSeconds, CardQR... cardQRs) throws WeixinException {

View File

@ -13,6 +13,7 @@ import com.foxinmy.weixin4j.http.weixin.ApiResult;
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
import com.foxinmy.weixin4j.mp.component.WeixinComponentPreCodeCreator;
import com.foxinmy.weixin4j.mp.component.WeixinComponentTokenCreator;
import com.foxinmy.weixin4j.mp.component.WeixinTokenComponentCreator;
import com.foxinmy.weixin4j.mp.model.AuthorizerOption;
import com.foxinmy.weixin4j.mp.model.AuthorizerOption.AuthorizerOptionName;
import com.foxinmy.weixin4j.mp.model.ComponentAuthorizer;
@ -20,6 +21,7 @@ import com.foxinmy.weixin4j.mp.model.ComponentAuthorizerToken;
import com.foxinmy.weixin4j.mp.model.OauthToken;
import com.foxinmy.weixin4j.token.PerTicketManager;
import com.foxinmy.weixin4j.token.TicketManager;
import com.foxinmy.weixin4j.token.TokenCreator;
import com.foxinmy.weixin4j.token.TokenManager;
import com.foxinmy.weixin4j.util.Consts;
import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
@ -32,7 +34,8 @@ import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
* @date 2015年6月17日
* @since JDK 1.6
* @see <a href=
* "https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1453779503&token=&lang=zh_CN">第三方应用组件概述</a>
* "https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1453779503&token=&lang=zh_CN">
* 第三方应用组件概述</a>
*/
public class ComponentApi extends MpApi {
/**
@ -55,11 +58,10 @@ public class ComponentApi extends MpApi {
*/
public ComponentApi(TicketManager ticketManager) {
this.ticketManager = ticketManager;
this.tokenManager = new TokenManager(new WeixinComponentTokenCreator(
ticketManager), ticketManager.getCacheStorager());
this.tokenManager = new TokenManager(new WeixinComponentTokenCreator(ticketManager),
ticketManager.getCacheStorager());
this.preCodeManager = new TokenManager(
new WeixinComponentPreCodeCreator(tokenManager,
ticketManager.getThirdId()),
new WeixinComponentPreCodeCreator(tokenManager, ticketManager.getThirdId()),
ticketManager.getCacheStorager());
}
@ -99,28 +101,26 @@ public class ComponentApi extends MpApi {
* @return 应用组件的perticket管理
*/
public PerTicketManager getRefreshTokenManager(String authAppId) {
return new PerTicketManager(authAppId, ticketManager.getThirdId(),
ticketManager.getThirdSecret(),
return new PerTicketManager(authAppId, ticketManager.getThirdId(), ticketManager.getThirdSecret(),
ticketManager.getCacheStorager());
}
/**
* 第三方组件代替授权公众号发起网页授权获取code <li>
* redirectUri默认填写weixin4j.properties#component.user.oauth.redirect.uri <li>
* scope默认填写snsapi_base <li>
* state默认填写state
* 第三方组件代替授权公众号发起网页授权获取code
* <li>redirectUri默认填写weixin4j.properties#component.user.oauth.redirect.uri
* <li>scope默认填写snsapi_base
* <li>state默认填写state
*
* @param authAppId
* 公众号的appid
* @see #getAuthorizationURL(String, String, String, String)
* @see <a
* href="https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419318590&token=&lang=zh_CN">第三方组件代替授权公众号发起网页授权</a>
* @see <a href=
* "https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419318590&token=&lang=zh_CN">
* 第三方组件代替授权公众号发起网页授权</a>
*/
public String getUserAuthorizationURL(String authAppId) {
String redirectUri = Weixin4jConfigUtil
.getValue("component.user.oauth.redirect.uri");
return getUserAuthorizationURL(authAppId, redirectUri, "snsapi_base",
"state");
String redirectUri = Weixin4jConfigUtil.getValue("component.user.oauth.redirect.uri");
return getUserAuthorizationURL(authAppId, redirectUri, "snsapi_base", "state");
}
/**
@ -135,16 +135,15 @@ public class ComponentApi extends MpApi {
* @param state
* 重定向后会带上state参数开发者可以填写任意参数值最多128字节
* @return oauth授权URL
* @see <a
* href="https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419318590&token=&lang=zh_CN">第三方组件代替授权公众号发起网页授权</a>
* @see <a href=
* "https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419318590&token=&lang=zh_CN">
* 第三方组件代替授权公众号发起网页授权</a>
*/
public String getUserAuthorizationURL(String authAppId, String redirectUri,
String scope, String state) {
public String getUserAuthorizationURL(String authAppId, String redirectUri, String scope, String state) {
String sns_component_user_auth_uri = getRequestUri("sns_component_user_auth_uri");
try {
return String.format(sns_component_user_auth_uri, authAppId,
URLEncoder.encode(redirectUri, Consts.UTF_8.name()), scope,
state, this.ticketManager.getThirdId());
URLEncoder.encode(redirectUri, Consts.UTF_8.name()), scope, state, this.ticketManager.getThirdId());
} catch (UnsupportedEncodingException e) {
;
}
@ -163,16 +162,13 @@ public class ComponentApi extends MpApi {
* @see com.foxinmy.weixin4j.mp.model.OauthToken
* @throws WeixinException
*/
public OauthToken getAuthorizationToken(String authAppId, String code)
throws WeixinException {
public OauthToken getAuthorizationToken(String authAppId, String code) throws WeixinException {
String sns_component_user_token_uri = getRequestUri("sns_component_user_token_uri");
String accessToken = tokenManager.getAccessToken();
WeixinResponse response = weixinExecutor.get(String.format(
sns_component_user_token_uri, authAppId, code,
ticketManager.getThirdId(), accessToken));
WeixinResponse response = weixinExecutor.get(
String.format(sns_component_user_token_uri, authAppId, code, ticketManager.getThirdId(), accessToken));
JSONObject result = response.getAsJson();
OauthToken token = new OauthToken(result.getString("access_token"),
result.getLongValue("expires_in") * 1000l);
OauthToken token = new OauthToken(result.getString("access_token"), result.getLongValue("expires_in") * 1000l);
token.setOpenId(result.getString("openid"));
token.setScope(result.getString("scope"));
token.setRefreshToken(result.getString("refresh_token"));
@ -192,16 +188,13 @@ public class ComponentApi extends MpApi {
* @see com.foxinmy.weixin4j.mp.model.OauthToken
* @throws WeixinException
*/
public OauthToken refreshAuthorizationToken(String authAppId,
String refreshToken) throws WeixinException {
public OauthToken refreshAuthorizationToken(String authAppId, String refreshToken) throws WeixinException {
String sns_component_token_refresh_uri = getRequestUri("sns_component_token_refresh_uri");
String accessToken = tokenManager.getAccessToken();
WeixinResponse response = weixinExecutor.get(String.format(
sns_component_token_refresh_uri, authAppId,
WeixinResponse response = weixinExecutor.get(String.format(sns_component_token_refresh_uri, authAppId,
ticketManager.getThirdId(), accessToken, refreshToken));
JSONObject result = response.getAsJson();
OauthToken token = new OauthToken(result.getString("access_token"),
result.getLongValue("expires_in") * 1000l);
OauthToken token = new OauthToken(result.getString("access_token"), result.getLongValue("expires_in") * 1000l);
token.setOpenId(result.getString("openid"));
token.setScope(result.getString("scope"));
token.setRefreshToken(result.getString("refresh_token"));
@ -222,61 +215,31 @@ public class ComponentApi extends MpApi {
* @see com.foxinmy.weixin4j.mp.model.ComponentAuthorizerToken
* @throws WeixinException
*/
public ComponentAuthorizerToken exchangeAuthorizerToken(String authCode)
throws WeixinException {
public ComponentAuthorizerToken exchangeAuthorizerToken(String authCode) throws WeixinException {
String component_exchange_authorizer_uri = getRequestUri("component_query_authorization_uri");
JSONObject obj = new JSONObject();
obj.put("component_appid", ticketManager.getThirdId());
obj.put("authorization_code", authCode);
WeixinResponse response = weixinExecutor.post(
String.format(component_exchange_authorizer_uri,
tokenManager.getAccessToken()), obj.toJSONString());
obj = response.getAsJson();
JSONObject authObj = obj.getJSONObject("authorization_info");
String.format(component_exchange_authorizer_uri, tokenManager.getAccessToken()), obj.toJSONString());
JSONObject authObj = response.getAsJson().getJSONObject("authorization_info");
JSONArray privilegesObj = authObj.getJSONArray("func_info");
List<Integer> privileges = new ArrayList<Integer>(privilegesObj.size());
for (int i = 0; i < privilegesObj.size(); i++) {
privileges.add(privilegesObj.getJSONObject(i)
.getJSONObject("funcscope_category").getInteger("id"));
privileges.add(privilegesObj.getJSONObject(i).getJSONObject("funcscope_category").getInteger("id"));
}
ComponentAuthorizerToken token = new ComponentAuthorizerToken(
authObj.getString("authorizer_access_token"),
authObj.getLongValue("expires_in"));
ComponentAuthorizerToken token = new ComponentAuthorizerToken(authObj.getString("authorizer_access_token"),
authObj.getLongValue("expires_in") * 1000l);
token.setRefreshToken(authObj.getString("authorizer_refresh_token"));
token.setPrivileges(privileges);
token.setAppId(authObj.getString("authorizer_appid"));
return token;
}
/**
* 获取刷新授权公众号或小程序的接口调用凭据令牌
*
* @param authAppId
* 授权方appid
* @param authRefreshToken
* 授权方的刷新令牌刷新令牌主要用于第三方平台获取和刷新已授权用户的access_token只会在授权时刻提供请妥善保存
* 一旦丢失只能让用户重新授权才能再次拿到新的刷新令牌
* @return 第三方组件授权信息
* @see {@link exchangeAuthInfo(String)}
* @see com.foxinmy.weixin4j.mp.model.ComponentAuthorizerToken
* @throws WeixinException
*/
public ComponentAuthorizerToken refreshAuthorizerToken(String authAppId,
String authRefreshToken) throws WeixinException {
String component_refresh_authorizer_token_uri = getRequestUri("component_refresh_authorizer_token_uri");
JSONObject obj = new JSONObject();
obj.put("component_appid", ticketManager.getThirdId());
obj.put("authorizer_appid", authAppId);
obj.put("authorizer_refresh_token", authRefreshToken);
WeixinResponse response = weixinExecutor.post(String.format(
component_refresh_authorizer_token_uri,
tokenManager.getAccessToken()), obj.toJSONString());
obj = response.getAsJson();
ComponentAuthorizerToken token = new ComponentAuthorizerToken(
obj.getString("authorizer_access_token"),
obj.getLongValue("expires_in"));
token.setRefreshToken(obj.getString("authorizer_refresh_token"));
token.setAppId(authAppId);
// 微信授权公众号的永久刷新令牌
PerTicketManager perTicketManager = getRefreshTokenManager(token.getAppId());
// 缓存微信公众号的access_token
TokenCreator tokenCreator = new WeixinTokenComponentCreator(perTicketManager, tokenManager);
ticketManager.getCacheStorager().caching(tokenCreator.key(), token);
// 缓存微信公众号的永久授权码(refresh_token)
perTicketManager.cachingTicket(token.getRefreshToken());
return token;
}
@ -291,29 +254,23 @@ public class ComponentApi extends MpApi {
* @see com.foxinmy.weixin4j.mp.model.ComponentAuthorizer
* @throws WeixinException
*/
public ComponentAuthorizer getAuthorizerInfo(String authAppId)
throws WeixinException {
public ComponentAuthorizer getAuthorizerInfo(String authAppId) throws WeixinException {
String component_get_authorizer_uri = getRequestUri("component_get_authorizer_uri");
JSONObject obj = new JSONObject();
obj.put("component_appid", ticketManager.getThirdId());
obj.put("authorizer_appid", authAppId);
WeixinResponse response = weixinExecutor.post(
String.format(component_get_authorizer_uri,
tokenManager.getAccessToken()), obj.toJSONString());
WeixinResponse response = weixinExecutor
.post(String.format(component_get_authorizer_uri, tokenManager.getAccessToken()), obj.toJSONString());
obj = response.getAsJson();
JSONObject auth = obj.getJSONObject("authorizer_info");
ComponentAuthorizer authorizer = JSON.toJavaObject(auth,
ComponentAuthorizer.class);
authorizer.setServiceType(auth.getJSONObject("service_type_info")
.getIntValue("id"));
authorizer.setVerifyType(auth.getJSONObject("verify_type_info")
.getIntValue("id"));
ComponentAuthorizer authorizer = JSON.toJavaObject(auth, ComponentAuthorizer.class);
authorizer.setServiceType(auth.getJSONObject("service_type_info").getIntValue("id"));
authorizer.setVerifyType(auth.getJSONObject("verify_type_info").getIntValue("id"));
auth = obj.getJSONObject("authorization_info");
JSONArray privilegesObj = auth.getJSONArray("func_info");
List<Integer> privileges = new ArrayList<Integer>(privilegesObj.size());
for (int i = 0; i < privilegesObj.size(); i++) {
privileges.add(privilegesObj.getJSONObject(i)
.getJSONObject("funcscope_category").getInteger("id"));
privileges.add(privilegesObj.getJSONObject(i).getJSONObject("funcscope_category").getInteger("id"));
}
authorizer.setPrivileges(privileges);
authorizer.setAppId(auth.getString("appid"));
@ -331,16 +288,15 @@ public class ComponentApi extends MpApi {
* @see com.foxinmy.weixin4j.mp.model.AuthorizerOption
* @throws WeixinException
*/
public AuthorizerOption getAuthorizerOption(String authAppId,
AuthorizerOptionName optionName) throws WeixinException {
public AuthorizerOption getAuthorizerOption(String authAppId, AuthorizerOptionName optionName)
throws WeixinException {
String component_get_authorizer_option_uri = getRequestUri("component_get_authorizer_option_uri");
JSONObject obj = new JSONObject();
obj.put("component_appid", ticketManager.getThirdId());
obj.put("authorizer_appid", authAppId);
obj.put("option_name", optionName.name());
WeixinResponse response = weixinExecutor.post(
String.format(component_get_authorizer_option_uri,
tokenManager.getAccessToken()), obj.toJSONString());
String.format(component_get_authorizer_option_uri, tokenManager.getAccessToken()), obj.toJSONString());
int optionValue = response.getAsJson().getIntValue("option_value");
return AuthorizerOption.parse(optionName, optionValue);
}
@ -354,8 +310,7 @@ public class ComponentApi extends MpApi {
* @see com.foxinmy.weixin4j.mp.model.AuthorizerOption
* @throws WeixinException
*/
public ApiResult setAuthorizerOption(String authAppId,
AuthorizerOption option) throws WeixinException {
public ApiResult setAuthorizerOption(String authAppId, AuthorizerOption option) throws WeixinException {
String component_set_authorizer_option_uri = getRequestUri("component_set_authorizer_option_uri");
JSONObject obj = new JSONObject();
obj.put("component_appid", ticketManager.getThirdId());
@ -363,8 +318,7 @@ public class ComponentApi extends MpApi {
obj.put("option_name", option.getName());
obj.put("option_value", option.getValue());
WeixinResponse response = weixinExecutor.post(
String.format(component_set_authorizer_option_uri,
tokenManager.getAccessToken()), obj.toJSONString());
String.format(component_set_authorizer_option_uri, tokenManager.getAccessToken()), obj.toJSONString());
return response.getAsResult();
}
}

View File

@ -55,7 +55,7 @@ public class WeixinTokenComponentCreator extends TokenCreator {
String.format(URLConsts.TOKEN_COMPONENT_URL, componentTokenManager.getAccessToken()),
obj.toJSONString());
obj = response.getAsJson();
return new Token(obj.getString("access_token"), obj.getLongValue("expires_in") * 1000l);
perTicketManager.cachingTicket(obj.getString("authorizer_refresh_token"));
return new Token(obj.getString("authorizer_access_token"), obj.getLongValue("expires_in") * 1000l);
}
}