issue #39 企业号添加oauth2的支持
This commit is contained in:
@@ -27,6 +27,8 @@ public interface WxCpConfigStorage {
|
||||
|
||||
public int getExpiresIn();
|
||||
|
||||
public String getOauth2redirectUri();
|
||||
|
||||
public String getHttp_proxy_host();
|
||||
|
||||
public int getHttp_proxy_port();
|
||||
|
||||
@@ -18,6 +18,8 @@ public class WxCpInMemoryConfigStorage implements WxCpConfigStorage {
|
||||
protected String agentId;
|
||||
protected int expiresIn;
|
||||
|
||||
protected String oauth2redirectUri;
|
||||
|
||||
protected String http_proxy_host;
|
||||
protected int http_proxy_port;
|
||||
protected String http_proxy_username;
|
||||
@@ -88,6 +90,15 @@ public class WxCpInMemoryConfigStorage implements WxCpConfigStorage {
|
||||
this.agentId = agentId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getOauth2redirectUri() {
|
||||
return this.oauth2redirectUri;
|
||||
}
|
||||
|
||||
public void setOauth2redirectUri(String oauth2redirectUri) {
|
||||
this.oauth2redirectUri = oauth2redirectUri;
|
||||
}
|
||||
|
||||
public String getHttp_proxy_host() {
|
||||
return http_proxy_host;
|
||||
}
|
||||
|
||||
@@ -274,6 +274,26 @@ public interface WxCpService {
|
||||
*/
|
||||
public void tagAddUsers(String tagId, List<String> userIds) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 构造oauth2授权的url连接
|
||||
* 详情请见: http://qydev.weixin.qq.com/wiki/index.php?title=企业获取code
|
||||
* </pre>
|
||||
* @param state
|
||||
* @return code
|
||||
*/
|
||||
public String oauth2buildAuthorizationUrl(String state);
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 用oauth2获取用户信息
|
||||
* http://qydev.weixin.qq.com/wiki/index.php?title=根据code获取成员信息
|
||||
* </pre>
|
||||
* @param code
|
||||
* @return [userid, deviceid]
|
||||
*/
|
||||
public String[] oauth2getUserInfo(String code) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 移除标签成员
|
||||
*
|
||||
|
||||
@@ -316,6 +316,41 @@ public class WxCpServiceImpl implements WxCpService {
|
||||
execute(new SimplePostRequestExecutor(), url, jsonObject.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String oauth2buildAuthorizationUrl(String state) {
|
||||
String url = "https://open.weixin.qq.com/connect/oauth2/authorize?" ;
|
||||
url += "appid=" + wxCpConfigStorage.getCorpId();
|
||||
url += "&redirect_uri=" + URIUtil.encodeURIComponent(wxCpConfigStorage.getOauth2redirectUri());
|
||||
url += "&response_type=code";
|
||||
url += "&scope=snsapi_base";
|
||||
if (state != null) {
|
||||
url += "&state=" + state;
|
||||
}
|
||||
url += "#wechat_redirect";
|
||||
return url;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] oauth2getUserInfo(String code) throws WxErrorException {
|
||||
String url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?";
|
||||
url += "access_token=" + wxCpConfigStorage.getAccessToken();
|
||||
url += "&code=" + code;
|
||||
url += "agendid=" + wxCpConfigStorage.getAgentId();
|
||||
|
||||
try {
|
||||
RequestExecutor<String, String> executor = new SimpleGetRequestExecutor();
|
||||
String responseText = executor.execute(getHttpclient(), httpProxy, url, null);
|
||||
JsonElement je = Streams.parse(new JsonReader(new StringReader(responseText)));
|
||||
JsonObject jo = je.getAsJsonObject();
|
||||
return new String[] {GsonHelper.getString(jo, "UserId"), GsonHelper.getString(jo, "DeviceId")};
|
||||
} catch (ClientProtocolException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public String get(String url, String queryParam) throws WxErrorException {
|
||||
return execute(new SimpleGetRequestExecutor(), url, queryParam);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user