issue #39 公众号添加oauth2的支持

This commit is contained in:
Daniel Qian
2014-11-28 15:57:52 +08:00
parent 8e4077e061
commit 0d6712f709
11 changed files with 267 additions and 80 deletions

View File

@@ -25,7 +25,7 @@ public interface WxMpConfigStorage {
public int getExpiresIn();
public String getOauth2redirectUrl();
public String getOauth2redirectUri();
public String getHttp_proxy_host();

View File

@@ -20,7 +20,7 @@ public class WxMpInMemoryConfigStorage implements WxMpConfigStorage {
protected int http_proxy_port;
protected String http_proxy_username;
protected String http_proxy_password;
protected String oauth2redirectUrl;
protected String oauth2redirectUri;
public void updateAccessToken(WxAccessToken accessToken) {
updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
@@ -80,12 +80,12 @@ public class WxMpInMemoryConfigStorage implements WxMpConfigStorage {
}
@Override
public String getOauth2redirectUrl() {
return this.oauth2redirectUrl;
public String getOauth2redirectUri() {
return this.oauth2redirectUri;
}
public void setOauth2redirectUrl(String oauth2redirectUrl) {
this.oauth2redirectUrl = oauth2redirectUrl;
public void setOauth2redirectUri(String oauth2redirectUri) {
this.oauth2redirectUri = oauth2redirectUri;
}
public String getHttp_proxy_host() {

View File

@@ -27,13 +27,11 @@ import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import javax.print.DocFlavor;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
@@ -303,7 +301,7 @@ public class WxMpServiceImpl implements WxMpService {
public String oauth2buildAuthorizationUrl(String scope, String state) {
String url = "https://open.weixin.qq.com/connect/oauth2/authorize?" ;
url += "appid=" + wxMpConfigStorage.getAppId();
url += "&redirect_uri=" + URIUtil.encodeURIComponent(wxMpConfigStorage.getOauth2redirectUrl());
url += "&redirect_uri=" + URIUtil.encodeURIComponent(wxMpConfigStorage.getOauth2redirectUri());
url += "&response_type=code";
url += "&scope=" + scope;
if (state != null) {

View File

@@ -1,6 +1,7 @@
package me.chanjar.weixin.mp.bean.result;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
/**
* Created by qianjia on 14/11/26.
@@ -58,7 +59,17 @@ public class WxMpOAuth2AccessToken {
}
public static WxMpOAuth2AccessToken fromJson(String json) {
return WxGsonBuilder.create().fromJson(json, WxMpOAuth2AccessToken.class);
return WxMpGsonBuilder.create().fromJson(json, WxMpOAuth2AccessToken.class);
}
@Override
public String toString() {
return "WxMpOAuth2AccessToken{" +
"accessToken='" + accessToken + '\'' +
", expiresIn=" + expiresIn +
", refreshToken='" + refreshToken + '\'' +
", openId='" + openId + '\'' +
", scope='" + scope + '\'' +
'}';
}
}

View File

@@ -9,7 +9,7 @@ import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
*/
public class WxMpUser {
protected boolean subscribe;
protected Boolean subscribe;
protected String openId;
protected String nickname;
protected String sex;
@@ -18,13 +18,13 @@ public class WxMpUser {
protected String province;
protected String country;
protected String headImgUrl;
protected long subscribeTime;
protected Long subscribeTime;
protected String unionId;
public boolean isSubscribe() {
public Boolean isSubscribe() {
return subscribe;
}
public void setSubscribe(boolean subscribe) {
public void setSubscribe(Boolean subscribe) {
this.subscribe = subscribe;
}
public String getOpenId() {
@@ -75,10 +75,10 @@ public class WxMpUser {
public void setHeadImgUrl(String headImgUrl) {
this.headImgUrl = headImgUrl;
}
public long getSubscribeTime() {
public Long getSubscribeTime() {
return subscribeTime;
}
public void setSubscribeTime(long subscribeTime) {
public void setSubscribeTime(Long subscribeTime) {
this.subscribeTime = subscribeTime;
}
public String getUnionId() {
@@ -91,5 +91,21 @@ public class WxMpUser {
public static WxMpUser fromJson(String json) {
return WxMpGsonBuilder.INSTANCE.create().fromJson(json, WxMpUser.class);
}
@Override
public String toString() {
return "WxMpUser{" +
"subscribe=" + subscribe +
", openId='" + openId + '\'' +
", nickname='" + nickname + '\'' +
", sex='" + sex + '\'' +
", language='" + language + '\'' +
", city='" + city + '\'' +
", province='" + province + '\'' +
", country='" + country + '\'' +
", headImgUrl='" + headImgUrl + '\'' +
", subscribeTime=" + subscribeTime +
", unionId='" + unionId + '\'' +
'}';
}
}

View File

@@ -4,6 +4,7 @@ import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import me.chanjar.weixin.mp.bean.*;
import me.chanjar.weixin.mp.bean.result.*;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
public class WxMpGsonBuilder {
@@ -24,6 +25,7 @@ public class WxMpGsonBuilder {
INSTANCE.registerTypeAdapter(WxMpQrCodeTicket.class, new WxQrCodeTicketAdapter());
INSTANCE.registerTypeAdapter(WxMpTemplateMessage.class, new WxMpTemplateMessageGsonAdapter());
INSTANCE.registerTypeAdapter(WxMpSemanticQueryResult.class, new WxMpSemanticQueryResultAdapter());
INSTANCE.registerTypeAdapter(WxMpOAuth2AccessToken.class, new WxMpOAuth2AccessTokenAdapter());
}
public static Gson create() {

View File

@@ -0,0 +1,38 @@
package me.chanjar.weixin.mp.util.json;
import com.google.gson.*;
import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.util.json.GsonHelper;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import java.lang.reflect.Type;
/**
* Created by qianjia on 14/11/28.
*/
public class WxMpOAuth2AccessTokenAdapter implements JsonDeserializer<WxMpOAuth2AccessToken> {
public WxMpOAuth2AccessToken deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws
JsonParseException {
WxMpOAuth2AccessToken accessToken = new WxMpOAuth2AccessToken();
JsonObject accessTokenJsonObject = json.getAsJsonObject();
if (accessTokenJsonObject.get("access_token") != null && !accessTokenJsonObject.get("access_token").isJsonNull()) {
accessToken.setAccessToken(GsonHelper.getAsString(accessTokenJsonObject.get("access_token")));
}
if (accessTokenJsonObject.get("expires_in") != null && !accessTokenJsonObject.get("expires_in").isJsonNull()) {
accessToken.setExpiresIn(GsonHelper.getAsPrimitiveInt(accessTokenJsonObject.get("expires_in")));
}
if (accessTokenJsonObject.get("refresh_token") != null && !accessTokenJsonObject.get("refresh_token").isJsonNull()) {
accessToken.setRefreshToken(GsonHelper.getAsString(accessTokenJsonObject.get("refresh_token")));
}
if (accessTokenJsonObject.get("openid") != null && !accessTokenJsonObject.get("openid").isJsonNull()) {
accessToken.setOpenId(GsonHelper.getAsString(accessTokenJsonObject.get("openid")));
}
if (accessTokenJsonObject.get("scope") != null && !accessTokenJsonObject.get("scope").isJsonNull()) {
accessToken.setScope(GsonHelper.getAsString(accessTokenJsonObject.get("scope")));
}
return accessToken;
}
}