#697 企业微信OAuth2.0增加对snsapi_userinfo和snsapi_privateinfo的支持

This commit is contained in:
Binary Wang 2018-08-04 19:34:42 +08:00
parent 013835fc31
commit c29a3e5f01
3 changed files with 35 additions and 9 deletions

View File

@ -234,10 +234,16 @@ public class WxConsts {
* 不弹出授权页面直接跳转只能获取用户openid.
*/
public static final String SNSAPI_BASE = "snsapi_base";
/**
* 弹出授权页面可通过openid拿到昵称性别所在地并且即使在未关注的情况下只要用户授权也能获取其信息.
*/
public static final String SNSAPI_USERINFO = "snsapi_userinfo";
/**
* 手动授权,可获取成员的详细信息,包含手机邮箱只适用于企业微信或企业号.
*/
public static final String SNSAPI_PRIVATEINFO = "snsapi_privateinfo";
}
/**

View File

@ -34,6 +34,19 @@ public interface WxCpOAuth2Service {
*/
String buildAuthorizationUrl(String redirectUri, String state);
/**
* <pre>
* 构造oauth2授权的url连接
* 详情请见: http://qydev.weixin.qq.com/wiki/index.php?title=企业获取code
* </pre>
*
* @param redirectUri 跳转链接地址
* @param state 状态码
* @param scope 取值参考me.chanjar.weixin.common.api.WxConsts.OAuth2Scope类
* @return url
*/
String buildAuthorizationUrl(String redirectUri, String state, String scope);
/**
* <pre>
* 用oauth2获取用户信息
@ -78,7 +91,7 @@ public interface WxCpOAuth2Service {
* 需要有对应应用的使用权限且成员必须在授权应用的可见范围内
* </pre>
*
* @param userTicket 成员票据
* @param userTicket 成员票据
*/
WxCpUserDetail getUserDetail(String userTicket) throws WxErrorException;
}

View File

@ -4,6 +4,7 @@ import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.URIUtil;
import me.chanjar.weixin.common.util.json.GsonHelper;
@ -37,16 +38,22 @@ public class WxCpOAuth2ServiceImpl implements WxCpOAuth2Service {
@Override
public String buildAuthorizationUrl(String redirectUri, String state) {
String url = "https://open.weixin.qq.com/connect/oauth2/authorize?";
url += "appid=" + this.mainService.getWxCpConfigStorage().getCorpId();
url += "&redirect_uri=" + URIUtil.encodeURIComponent(redirectUri);
url += "&response_type=code";
url += "&scope=snsapi_base";
return this.buildAuthorizationUrl(redirectUri, state, WxConsts.OAuth2Scope.SNSAPI_BASE);
}
@Override
public String buildAuthorizationUrl(String redirectUri, String state, String scope) {
StringBuilder url = new StringBuilder("https://open.weixin.qq.com/connect/oauth2/authorize?");
url.append("appid=").append(this.mainService.getWxCpConfigStorage().getCorpId());
url.append("&redirect_uri=").append(URIUtil.encodeURIComponent(redirectUri));
url.append("&response_type=code");
url.append("&scope=").append(scope);
if (state != null) {
url += "&state=" + state;
url.append("&state=").append(state);
}
url += "#wechat_redirect";
return url;
url.append("#wechat_redirect");
return url.toString();
}
@Override