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 * 企业号oauth授权
12 *
13 * @className OauthApi
14 * @author jinyu(foxinmy@gmail.com)
15 * @date 2015年6月11日
16 * @since JDK 1.6
17 * @see <a href= "http://work.weixin.qq.com/api/doc#10028"> 企业号用户身份授权说明</a>
18 * @see <a href=
19 * "http://work.weixin.qq.com/api/doc#11791/服务商的凭证">企业号第三方提供商授权说明</a>
20 * @see <a href= "http://work.weixin.qq.com/api/doc#10974"> 企业号第三方套件应用授权说明</a>
21 */
22 public class OauthApi extends QyApi {
23 private final WeixinAccount account;
24
25 /**
26 * 默认使用weixin4j.properties里面的corpid、corpsecret信息
27 */
28 public OauthApi() {
29 this(Weixin4jConfigUtil.getWeixinAccount());
30 }
31
32 /**
33 * 传入corpid、appsecret信息
34 *
35 * @param account
36 */
37 public OauthApi(WeixinAccount account) {
38 this.account = account;
39 }
40
41 /**
42 * 企业号成员身份授权<li>
43 * redirectUri默认填写weixin4j.properties#user.oauth.redirect.uri <li>
44 * state默认填写state
45 *
46 * @see {@link #getUserAuthorizationURL(String,String)}
47 *
48 * @return 请求授权的URL
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 * @param redirectUri
61 * 重定向地址
62 * @param scope
63 * 应用授权作用域。 snsapi_base:静默授权,可获取成员的基础信息;</br>
64 * snsapi_userinfo:静默授权,可获取成员的详细信息,但不包含手机、邮箱;</br>
65 * snsapi_privateinfo:手动授权,可获取成员的详细信息,包含手机、邮箱。</br>
66 * @param state
67 * 用于保持请求和回调的状态
68 * @param agentId
69 * 企业应用的id。 当scope是snsapi_userinfo或snsapi_privateinfo时,该参数必填。
70 * 注意redirect_uri的域名必须与该应用的可信域名一致。
71 * @return 请求授权的URL
72 * @see UserApi#getOUserInfoByCode(String, String)
73 * @see <a href= "http://work.weixin.qq.com/api/doc#10028">企业号用户身份授权</a>
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 * 企业号第三方提供商登陆授权<li>
90 * redirectUri默认填写weixin4j.properties#third.oauth.redirect.uri <li>
91 * state默认填写state
92 *
93 * @see {@link #getUserThirdAuthorizationURL(String,String)}
94 *
95 * @return 请求授权的URL
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 * @param corpId
107 * 服务商的CorpID或者企业的CorpID
108 * @param redirectUri
109 * 重定向地址
110 * @param state
111 * 用于保持请求和回调的状态,授权请求后原样带回给第三方
112 * @param userType
113 * redirect_uri支持登录的类型,有member(成员登录)、admin(管理员登录)、all(成员或管理员皆可登录)
114 * ,默认值为admin
115 * @return 请求授权的URL
116 * @see ProviderApi#getOUserInfoByCode(String)
117 * 授权登录服务商的网站时,使用应用提供商的provider_access_token;
118 * @see UserApi#getOUserInfoByCode(String) 授权登录企业的网站时,使用企业管理组的access_token
119 * @see <a href=
120 * "http://qydev.weixin.qq.com/wiki/index.php?title=%E6%88%90%E5%91%98%E7%99%BB%E5%BD%95%E6%8E%88%E6%9D%83">
121 * 企业号第三方提供商授权</a>
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 }