1 package com.foxinmy.weixin4j.mp.api;
2
3 import java.io.UnsupportedEncodingException;
4 import java.net.URLEncoder;
5
6 import com.alibaba.fastjson.JSONObject;
7 import com.alibaba.fastjson.TypeReference;
8 import com.foxinmy.weixin4j.exception.WeixinException;
9 import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
10 import com.foxinmy.weixin4j.model.WeixinAccount;
11 import com.foxinmy.weixin4j.mp.model.OauthToken;
12 import com.foxinmy.weixin4j.mp.model.User;
13 import com.foxinmy.weixin4j.mp.type.Lang;
14 import com.foxinmy.weixin4j.util.Consts;
15 import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
16
17
18
19
20
21
22
23
24
25 public class OauthApi extends MpApi {
26
27 private final WeixinAccount account;
28
29
30
31
32 public OauthApi() {
33 this(Weixin4jConfigUtil.getWeixinAccount());
34 }
35
36
37
38
39
40
41 public OauthApi(WeixinAccount account) {
42 this.account = account;
43 }
44
45
46
47
48
49
50
51
52
53
54
55 public String getUserAuthorizationURL() {
56 String redirectUri = Weixin4jConfigUtil
57 .getValue("user.oauth.redirect.uri");
58 return getUserAuthorizationURL(redirectUri, "state", "snsapi_base");
59 }
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90 public String getUserAuthorizationURL(String redirectUri, String state,
91 String scope) {
92 String sns_user_auth_uri = getRequestUri("sns_user_auth_uri");
93 try {
94 return String.format(sns_user_auth_uri, account.getId(),
95 URLEncoder.encode(redirectUri, Consts.UTF_8.name()), scope,
96 state);
97 } catch (UnsupportedEncodingException e) {
98 ;
99 }
100 return "";
101 }
102
103
104
105
106
107
108
109
110
111
112
113
114 public OauthToken getAuthorizationToken(String code) throws WeixinException {
115 String user_token_uri = getRequestUri("sns_user_token_uri");
116 WeixinResponse response = weixinExecutor.get(String.format(
117 user_token_uri, account.getId(), account.getSecret(), code));
118 JSONObject result = response.getAsJson();
119 OauthToken token = new OauthToken(result.getString("access_token"),
120 result.getLongValue("expires_in") * 1000l);
121 token.setUnionId(result.getString("unionid"));
122 token.setOpenId(result.getString("openid"));
123 token.setScope(result.getString("scope"));
124 token.setRefreshToken(result.getString("refresh_token"));
125 return token;
126 }
127
128
129
130
131
132
133
134
135
136
137
138
139 public OauthToken refreshAuthorizationToken(String refreshToken)
140 throws WeixinException {
141 String sns_token_refresh_uri = getRequestUri("sns_token_refresh_uri");
142 WeixinResponse response = weixinExecutor.get(String.format(
143 sns_token_refresh_uri, account.getId(), refreshToken));
144 JSONObject result = response.getAsJson();
145 OauthToken token = new OauthToken(result.getString("access_token"),
146 result.getLongValue("expires_in") * 1000l);
147 token.setUnionId(result.getString("unionid"));
148 token.setOpenId(result.getString("openid"));
149 token.setScope(result.getString("scope"));
150 token.setRefreshToken(result.getString("refresh_token"));
151 return token;
152 }
153
154
155
156
157
158
159
160
161
162
163 public boolean verifyAuthorizationToken(String oauthToken, String openId) {
164 String sns_auth_token_uri = getRequestUri("sns_auth_token_uri");
165 try {
166 weixinExecutor.get(String.format(sns_auth_token_uri, oauthToken,
167 openId));
168 return true;
169 } catch (WeixinException e) {
170 ;
171 }
172 return false;
173 }
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188 public User getAuthorizationUser(OauthToken token) throws WeixinException {
189 return getAuthorizationUser(token.getAccessToken(), token.getOpenId(),
190 Lang.zh_CN);
191 }
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210 public User getAuthorizationUser(String oauthToken, String openid, Lang lang)
211 throws WeixinException {
212 String user_info_uri = getRequestUri("sns_user_info_uri");
213 WeixinResponse response = weixinExecutor.get(String.format(
214 user_info_uri, oauthToken, openid, lang.name()));
215
216 return response.getAsObject(new TypeReference<User>() {
217 });
218 }
219
220
221
222
223
224
225
226
227
228 public String getOpenAuthorizationURL() {
229 String redirectUri = Weixin4jConfigUtil
230 .getValue("open.user.oauth.redirect.uri");
231 return getOpenAuthorizationURL(redirectUri, "state");
232 }
233
234
235
236
237
238
239
240
241
242
243
244
245
246 public String getOpenAuthorizationURL(String redirectUri, String state) {
247 String open_user_auth_uri = getRequestUri("open_user_auth_uri");
248 try {
249 return String.format(open_user_auth_uri, account.getId(),
250 URLEncoder.encode(redirectUri, Consts.UTF_8.name()),
251 "snsapi_login", state);
252 } catch (UnsupportedEncodingException e) {
253 ;
254 }
255 return "";
256 }
257 }