issue #39 添加oauth2的支持

This commit is contained in:
Daniel Qian
2014-11-26 22:07:13 +08:00
parent 4d8f9ffd8e
commit 7b92a14f1c
7 changed files with 273 additions and 0 deletions

View File

@@ -128,4 +128,12 @@ public class WxConsts {
/** 弹出地理位置选择器 */
public static final String LOCATION_SELECT = "location_select";
///////////////////////
// oauth2网页授权的scope
///////////////////////
/** 不弹出授权页面直接跳转只能获取用户openid */
public static final String OAUTH2_SCOPE_BASE = "snsapi_base";
/** 弹出授权页面可通过openid拿到昵称、性别、所在地。并且即使在未关注的情况下只要用户授权也能获取其信息 */
public static final String OAUTH2_SCOPE_USER_INFO = "snsapi_userinfo";
}

View File

@@ -0,0 +1,47 @@
package me.chanjar.weixin.common.util.http;
import me.chanjar.weixin.common.util.StringUtils;
import java.io.UnsupportedEncodingException;
public class URIUtil {
private static final String ALLOWED_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.!~*'()";
public static String encodeURIComponent(String input) {
if (StringUtils.isEmpty(input)) {
return input;
}
int l = input.length();
StringBuilder o = new StringBuilder(l * 3);
try {
for (int i = 0; i < l; i++) {
String e = input.substring(i, i + 1);
if (ALLOWED_CHARS.indexOf(e) == -1) {
byte[] b = e.getBytes("utf-8");
o.append(getHex(b));
continue;
}
o.append(e);
}
return o.toString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return input;
}
private static String getHex(byte buf[]) {
StringBuilder o = new StringBuilder(buf.length * 3);
for (int i = 0; i < buf.length; i++) {
int n = (int) buf[i] & 0xff;
o.append("%");
if (n < 0x10) {
o.append("0");
}
o.append(Long.toString(n, 16).toUpperCase());
}
return o.toString();
}
}