// 开发者ID
private static final String APPID = "";
// 开发者密码
private static final String SECRET = "";
// 授权码发放类型
private static final String GRANT_TYPE = "client_credential";
// 授权码类型
private static final String TYPE = "jsapi";
// 获取微信 TOKEN 接口
private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token";
// 获取微信令牌接口
private static final String TICKET_URL = "https://api.weixin.qq.com/cgi-bin/ticket/getticket";
// 请求参数名
private static final String ACCESS_TOKEN_KEY = "access_token";
private static final String TICKET_KEY = "ticket";
// JSON 解析
private static JsonParser parse = new JsonParser();
// 注意 URL 一定要动态获取,不能 hardcode
public static Map<String, String> weChatSignature(String url) {
String token = getToken();
Map<String, String> result = new HashMap<String, String>();
String ticket = getTicket(token);
String nonceStr = UUID.randomUUID().toString(); // 随机字符串
String timestamp = Long.toString(System.currentTimeMillis() / 1000); // 时间戳
//注意这里参数名必须全部小写,且必须有序
String context = "jsapi_ticket=" + ticket + "&noncestr=" + nonceStr +
"×tamp=" + timestamp + "&url=" + url;
String signature = SHA1(context);
result.put("url", url);
result.put("jsapi_ticket", ticket);
result.put("nonce_str", nonceStr);
result.put("timestamp", timestamp);
result.put("signature", signature);
result.put("appid", APPID);
return result;
}
// 获取 token
public static String getToken() {
// https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=&secret=
String url = ACCESS_TOKEN_URL + "?grant_type=" + GRANT_TYPE + "&appid=" + APPID + "&secret=" + SECRET;
String response = httpGetForJson(url);
return ((JsonObject) parse.parse(response)).get(ACCESS_TOKEN_KEY).getAsString();
}
// 获取 ticket
public static String getTicket(String token) {
// https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=&type=
String url = TICKET_URL + "?access_token=" + token + "&type=" + TYPE;
String response = httpGetForJson(url);
return ((JsonObject) parse.parse(response)).get(TICKET_KEY).getAsString();
}
// 发送请求
public static String httpGetForJson(String url) {
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
// 创建 HTTPGET
HttpGet httpget = new HttpGet(url);
// 发送 GET 请求
CloseableHttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
return entity == null ? "" : EntityUtils.toString(entity);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return "";
}
// 加密
public static String SHA1(final String value) {
MessageDigest digest;
try {
digest = MessageDigest.getInstance("SHA-1");
digest.update(value.getBytes());
byte messageDigest[] = digest.digest();
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < messageDigest.length; i++) {
String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
if (shaHex.length() < 2) {
hexString.append(0);
}
hexString.append(shaHex);
}
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return "";
}