Skip to content

Commit 41ab0c7

Browse files
committed
WxLiteOAuth added
1 parent 715a5da commit 41ab0c7

4 files changed

Lines changed: 157 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# ChangeLog
22

3+
## 2.3.8
4+
#### 新增
5+
- WxLiteOAuth 接口
6+
- Agreement 接口
7+
8+
#### 修改
9+
- 合并部分内部类
10+
- WxpubOAuth 接口错误时增加异常
11+
312
## 2.3.7 (2018-03-16)
413
#### 新增
514
- BalanceSettlement 接口
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.pingplusplus.util;
2+
3+
import com.google.gson.FieldNamingPolicy;
4+
import com.google.gson.GsonBuilder;
5+
import com.pingplusplus.exception.ChannelException;
6+
7+
import java.io.UnsupportedEncodingException;
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
11+
/**
12+
* 用于微信小程序用户授权后获取用户唯一标识 openid
13+
* WxLiteOAuth 中的方法都是可选的,开发者也可根据实际情况自行开发相关功能,
14+
* 详细内容可参考 https://developers.weixin.qq.com/miniprogram/dev/api/api-login.html
15+
*/
16+
public class WxLiteOAuth extends WxpubOAuth {
17+
/**
18+
* 获取微信小程序授权用户唯一标识
19+
*
20+
* @param appId 微信小程序应用唯一标识
21+
* @param appSecret 微信小程序应用密钥(注意保密)
22+
* @param code 授权 code, 登录时获取的 code
23+
* @return openid 微信小程序授权用户唯一标识
24+
* @throws UnsupportedEncodingException
25+
*/
26+
public static String getOpenId(String appId, String appSecret, String code)
27+
throws UnsupportedEncodingException, ChannelException {
28+
29+
AuthResult authResult = getSession(appId, appSecret, code);
30+
if (authResult.getErrmsg() != null) {
31+
throw new ChannelException(authResult.getErrmsg(), authResult.getErrcode().toString(), null);
32+
}
33+
34+
return authResult.getOpenid();
35+
}
36+
37+
/**
38+
* 获取微信小程序授权用户唯一标识
39+
*
40+
* @param appId 微信小程序应用唯一标识
41+
* @param appSecret 微信小程序应用密钥(注意保密)
42+
* @param code 授权 code, 登录时获取的 code
43+
* @return openid 微信小程序授权用户唯一标识
44+
* @throws UnsupportedEncodingException
45+
*/
46+
public static AuthResult getSession(String appId, String appSecret, String code)
47+
throws UnsupportedEncodingException {
48+
Map<String, String> params = new HashMap<String, String>();
49+
params.put("appid", appId);
50+
params.put("secret", appSecret);
51+
params.put("js_code", code);
52+
params.put("grant_type", "authorization_code");
53+
String url = "https://api.weixin.qq.com/sns/jscode2session?" + httpBuildQuery(params);
54+
55+
String ret = httpGet(url);
56+
AuthResult authResult = new GsonBuilder()
57+
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
58+
.create().fromJson(ret, AuthResult.class);
59+
60+
return authResult;
61+
}
62+
63+
class AuthResult {
64+
String sessionKey;
65+
String openid;
66+
String unionid;
67+
Integer errcode;
68+
String errmsg;
69+
70+
public String getSessionKey() {
71+
return sessionKey;
72+
}
73+
74+
public String getOpenid() {
75+
return openid;
76+
}
77+
78+
public String getUnionid() {
79+
return unionid;
80+
}
81+
82+
public Integer getErrcode() {
83+
return errcode;
84+
}
85+
86+
public String getErrmsg() {
87+
return errmsg;
88+
}
89+
}
90+
}

src/main/java/com/pingplusplus/util/WxpubOAuth.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.pingplusplus.util;
22

33
import com.google.gson.*;
4+
import com.pingplusplus.exception.ChannelException;
5+
import com.pingplusplus.model.Channel;
46

57
import java.io.BufferedReader;
68
import java.io.IOException;
@@ -18,7 +20,7 @@
1820
/**
1921
* 用于微信公众号OAuth2.0鉴权,用户授权后获取授权用户唯一标识openid
2022
* WxpubOAuth中的方法都是可选的,开发者也可根据实际情况自行开发相关功能,
21-
* 详细内容可参考http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html
23+
* 详细内容可参考 https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842
2224
*/
2325
public class WxpubOAuth {
2426

@@ -37,14 +39,18 @@ public class WxpubOAuth {
3739
* @throws UnsupportedEncodingException
3840
*/
3941
public static String getOpenId(String appId, String appSecret, String code)
40-
throws UnsupportedEncodingException {
42+
throws UnsupportedEncodingException, ChannelException {
4143
String url = WxpubOAuth.createOauthUrlForOpenid(appId, appSecret, code);
4244

4345
String ret = WxpubOAuth.httpGet(url);
4446
OAuthResult oAuthResult = new GsonBuilder()
4547
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
4648
.create().fromJson(ret, OAuthResult.class);
4749

50+
if (oAuthResult.getErrmsg() != null) {
51+
throw new ChannelException(oAuthResult.getErrmsg(), oAuthResult.getErrcode().toString(), null);
52+
}
53+
4854
return oAuthResult.getOpenid();
4955
}
5056

@@ -95,7 +101,7 @@ private static String createOauthUrlForOpenid(String appId, String appSecret, St
95101
return "https://api.weixin.qq.com/sns/oauth2/access_token?" + queryString;
96102
}
97103

98-
private static String httpBuildQuery(Map<String, String> queryString) throws UnsupportedEncodingException {
104+
protected static String httpBuildQuery(Map<String, String> queryString) throws UnsupportedEncodingException {
99105
StringBuilder sb = new StringBuilder();
100106
for (Map.Entry<String, String> e : queryString.entrySet()) {
101107
if (sb.length() > 0) {
@@ -112,7 +118,7 @@ private static String httpBuildQuery(Map<String, String> queryString) throws Uns
112118
* @param urlString
113119
* @return responseString
114120
*/
115-
private static String httpGet(String urlString) {
121+
protected static String httpGet(String urlString) {
116122
String result = "";
117123
try {
118124
URL url = new URL(urlString);
@@ -221,6 +227,8 @@ class OAuthResult {
221227
String refreshToken;
222228
String openid;
223229
String scope;
230+
Integer errcode;
231+
String errmsg;
224232

225233
public String getAccessToken() {
226234
return accessToken;
@@ -241,5 +249,13 @@ public String getOpenid() {
241249
public String getScope() {
242250
return scope;
243251
}
252+
253+
public Integer getErrcode() {
254+
return errcode;
255+
}
256+
257+
public String getErrmsg() {
258+
return errmsg;
259+
}
244260
}
245261
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.pingplusplus;
2+
3+
import com.pingplusplus.exception.ChannelException;
4+
import com.pingplusplus.util.WxLiteOAuth;
5+
import com.pingplusplus.util.WxpubOAuth;
6+
import org.junit.Test;
7+
8+
import java.io.UnsupportedEncodingException;
9+
10+
public class WeixinOpenidTest extends PingppTestBase {
11+
/**
12+
* 通过 appId, secret, code 获取微信公众号 openid 信息。
13+
*/
14+
@Test
15+
public void testWxPubOpenid() throws UnsupportedEncodingException {
16+
try {
17+
String openid = WxpubOAuth.getOpenId("wx262681902838", "piOgk852569gKXpRLjhh38J6O14H7ejb", "ZkJfiPkzQAFCxc4vxRE4");
18+
System.out.println(openid);
19+
} catch (ChannelException e) {
20+
System.out.println(e.getMessage());
21+
System.out.println(e.getParam());
22+
}
23+
}
24+
25+
/**
26+
* 通过 appId, secret, code 获取微信小程序 openid 信息。
27+
*/
28+
@Test
29+
public void testWxLiteOpenid() throws UnsupportedEncodingException {
30+
try {
31+
String openid = WxLiteOAuth.getOpenId("wx283881926260", "piOgk852569gKXpRLjhh38J6O14H7ejb", "vNnPjvqD0BT3snbxVLjY");
32+
System.out.println(openid);
33+
} catch (ChannelException e) {
34+
System.out.println(e.getMessage());
35+
System.out.println(e.getParam());
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)