1 package com.foxinmy.weixin4j.qy.api;
2
3 import java.io.UnsupportedEncodingException;
4 import java.net.URLEncoder;
5
6 import com.foxinmy.weixin4j.model.WeixinAccount;
7 import com.foxinmy.weixin4j.util.Consts;
8 import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
9
10
11
12
13
14
15
16
17
18
19
20
21
22 public class OauthApi extends QyApi {
23 private final WeixinAccount account;
24
25
26
27
28 public OauthApi() {
29 this(Weixin4jConfigUtil.getWeixinAccount());
30 }
31
32
33
34
35
36
37 public OauthApi(WeixinAccount account) {
38 this.account = account;
39 }
40
41
42
43
44
45
46
47
48
49
50 public String getUserAuthorizationURL() {
51 String redirectUri = Weixin4jConfigUtil
52 .getValue("user.oauth.redirect.uri");
53 return getUserAuthorizationURL(redirectUri, "snsapi_base", "state",
54 null);
55 }
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75 public String getUserAuthorizationURL(String redirectUri, String scope,
76 String state, Integer agentId) {
77 String oauth_uri = getRequestUri("user_oauth_uri");
78 try {
79 return String.format(oauth_uri, account.getId(),
80 URLEncoder.encode(redirectUri, Consts.UTF_8.name()), scope,
81 state, agentId != null ? agentId.intValue() : "");
82 } catch (UnsupportedEncodingException e) {
83 ;
84 }
85 return "";
86 }
87
88
89
90
91
92
93
94
95
96
97 public String getThirdAuthorizationURL() {
98 String redirectUri = Weixin4jConfigUtil
99 .getValue("third.oauth.redirect.uri");
100 return getUserThirdAuthorizationURL(redirectUri, "state", "all");
101 }
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123 public String getUserThirdAuthorizationURL(String redirectUri,
124 String state, String userType) {
125 String oauth_uri = getRequestUri("user_thirdoauth_uri");
126 try {
127 return String.format(oauth_uri, account.getId(),
128 URLEncoder.encode(redirectUri, Consts.UTF_8.name()), state,
129 userType);
130 } catch (UnsupportedEncodingException e) {
131 ;
132 }
133 return "";
134 }
135 }