Skip to content

Commit bd1cf2d

Browse files
committed
#1230 企业微信增加第三方应用获取服务商凭证凭证的接口
1 parent 4ff65a0 commit bd1cf2d

File tree

5 files changed

+72
-4
lines changed

5 files changed

+72
-4
lines changed

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpService.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
import me.chanjar.weixin.cp.bean.WxCpMaJsCode2SessionResult;
1111
import me.chanjar.weixin.cp.bean.WxCpMessage;
1212
import me.chanjar.weixin.cp.bean.WxCpMessageSendResult;
13+
import me.chanjar.weixin.cp.bean.WxCpProviderToken;
1314
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
1415

1516
/**
1617
* 微信API的Service.
18+
*
1719
* @author chanjaster
1820
*/
1921
public interface WxCpService {
@@ -72,9 +74,10 @@ public interface WxCpService {
7274
/**
7375
* 获得jsapi_ticket,不强制刷新jsapi_ticket
7476
* 应用的jsapi_ticket用于计算agentConfig(参见“通过agentConfig注入应用的权限”)的签名,签名计算方法与上述介绍的config的签名算法完全相同,但需要注意以下区别:
75-
*
77+
* <p>
7678
* 签名的jsapi_ticket必须使用以下接口获取。且必须用wx.agentConfig中的agentid对应的应用secret去获取access_token。
7779
* 签名用的noncestr和timestamp必须与wx.agentConfig中的nonceStr和timestamp相同。
80+
*
7881
* @see #getJsapiTicket(boolean)
7982
*/
8083
String getAgentJsapiTicket() throws WxErrorException;
@@ -134,6 +137,26 @@ public interface WxCpService {
134137
*/
135138
String[] getCallbackIp() throws WxErrorException;
136139

140+
/**
141+
* <pre>
142+
* 获取服务商凭证
143+
* 文档地址:https://work.weixin.qq.com/api/doc#90001/90143/91200
144+
* 请求方式:POST(HTTPS)
145+
* 请求地址: https://qyapi.weixin.qq.com/cgi-bin/service/get_provider_token
146+
* </pre>
147+
*
148+
* @param corpId 服务商的corpid
149+
* @param providerSecret 服务商的secret,在服务商管理后台可见
150+
* @return {
151+
* "errcode":0 ,
152+
* "errmsg":"ok" ,
153+
* "provider_access_token":"enLSZ5xxxxxxJRL",
154+
* "expires_in":7200
155+
* }
156+
* @throws WxErrorException .
157+
*/
158+
WxCpProviderToken getProviderToken(String corpId, String providerSecret) throws WxErrorException;
159+
137160
/**
138161
* 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的GET请求
139162
*
@@ -206,7 +229,7 @@ public interface WxCpService {
206229
* @return WxSessionManager
207230
*/
208231
WxSessionManager getSessionManager();
209-
232+
210233
/**
211234
* <pre>
212235
* 设置WxSessionManager,只有当需要使用个性化的WxSessionManager的时候才需要调用此方法,
@@ -289,7 +312,7 @@ public interface WxCpService {
289312

290313
/**
291314
* 获取群聊服务
292-
*
315+
*
293316
* @return 群聊服务
294317
*/
295318
WxCpChatService getChatService();

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/BaseWxCpServiceImpl.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import me.chanjar.weixin.cp.bean.WxCpMaJsCode2SessionResult;
2525
import me.chanjar.weixin.cp.bean.WxCpMessage;
2626
import me.chanjar.weixin.cp.bean.WxCpMessageSendResult;
27+
import me.chanjar.weixin.cp.bean.WxCpProviderToken;
2728
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
2829

2930
import java.io.File;
@@ -199,6 +200,14 @@ public String[] getCallbackIp() throws WxErrorException {
199200
return ips;
200201
}
201202

203+
@Override
204+
public WxCpProviderToken getProviderToken(String corpId, String providerSecret) throws WxErrorException {
205+
JsonObject jsonObject = new JsonObject();
206+
jsonObject.addProperty("corpid", corpId);
207+
jsonObject.addProperty("provider_secret", providerSecret);
208+
return WxCpProviderToken.fromJson(this.post(this.configStorage.getApiUrl(GET_PROVIDER_TOKEN), jsonObject.toString()));
209+
}
210+
202211
@Override
203212
public String get(String url, String queryParam) throws WxErrorException {
204213
return execute(SimpleGetRequestExecutor.create(this), url, queryParam);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package me.chanjar.weixin.cp.bean;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import lombok.Data;
5+
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
6+
7+
/**
8+
* 服务商凭证.
9+
*
10+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
11+
* @date 2019-11-02
12+
*/
13+
@Data
14+
public class WxCpProviderToken {
15+
/**
16+
* 服务商的access_token,最长为512字节。
17+
*/
18+
@SerializedName("provider_access_token")
19+
private String providerAccessToken;
20+
21+
/**
22+
* provider_access_token有效期(秒)
23+
*/
24+
@SerializedName("expires_in")
25+
private Integer expiresIn;
26+
27+
public static WxCpProviderToken fromJson(String json) {
28+
return WxCpGsonBuilder.create().fromJson(json, WxCpProviderToken.class);
29+
}
30+
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpApiPathConsts.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ public final class WxCpApiPathConsts {
2020
public static final String BATCH_REPLACE_USER = "/cgi-bin/batch/replaceuser";
2121
public static final String BATCH_GET_RESULT = "/cgi-bin/batch/getresult?jobid=";
2222
public static final String JSCODE_TO_SESSION = "/cgi-bin/miniprogram/jscode2session";
23-
public static final String GET_TOKEN = "/cgi-bin/gettoken?&corpid=%s&corpsecret=%s";
23+
public static final String GET_TOKEN = "/cgi-bin/gettoken?corpid=%s&corpsecret=%s";
24+
public static final String GET_PROVIDER_TOKEN = "/cgi-bin/service/get_provider_token";
2425

2526
public static class Agent {
2627
public static final String AGENT_GET = "/cgi-bin/agent/get?agentid=%d";

weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/BaseWxCpServiceImplTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,9 @@ public void testGetAgentJsapiTicket() throws WxErrorException {
3333
public void testJsCode2Session() throws WxErrorException {
3434
assertThat(this.wxService.jsCode2Session("111")).isNotNull();
3535
}
36+
37+
@Test
38+
public void testGetProviderToken() throws WxErrorException {
39+
assertThat(this.wxService.getProviderToken("111","123")).isNotNull();
40+
}
3641
}

0 commit comments

Comments
 (0)