1
0
mirror of synced 2025-12-22 00:48:00 +08:00

issue #39 企业号添加oauth2的支持

This commit is contained in:
Daniel Qian
2014-11-28 16:22:02 +08:00
parent 0d6712f709
commit 806447abf1
11 changed files with 206 additions and 48 deletions

View File

@@ -27,6 +27,8 @@ public interface WxCpConfigStorage {
public int getExpiresIn();
public String getOauth2redirectUri();
public String getHttp_proxy_host();
public int getHttp_proxy_port();

View File

@@ -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;
}

View File

@@ -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;
/**
* 移除标签成员
*

View File

@@ -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);
}