- 获取access_token
- 获取ticket
- 根据ticket换取该公众账号的二维码
1.获取access_oken
// 获取token接口(GET)
public final static String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
/**
* 获取接口访问凭证
* @param appid 凭证
* @param appsecret 密钥
*/
public static AccessToken getToken(String appid, String appsecret) {
AccessToken token = null;
String requestUrl = WxConstants.TOKEN_URL.replace("APPID", appid)
.replace("APPSECRET", appsecret);
// 发起GET请求获取凭证
JSONObject jsonObject = httpsRequestJson(requestUrl, "GET", null);
if (null != jsonObject) {
try {
token = new AccessToken();
token.setAccess_token(jsonObject.getString("access_token"));
token.setExpires_in(jsonObject.getInt("expires_in"));
} catch (JSONException e) {
token = null;
// 获取token失败
logger.error("获取token失败 errcode:{} errmsg:{}",
jsonObject.getInt("errcode"),
jsonObject.getString("errmsg"));
}
}
return token;
}
2.获取ticket
/**
* 创建临时带参数二维码
* @param accessToken
* @expireSeconds 该二维码有效时间,以秒为单位。 最大不超过2592000(即30天),此字段如果不填,则默认有效期为30秒。
* @param sceneId 场景Id
* @return
*/
public static WeChatQRCode createTemporaryQRCode(String accessToken, String expireSeconds ,int sceneId) {
WeChatQRCode weChatQRCode = null;
String requestUrl = WxConstants.QR_CODE_URL.replace("TOKEN", accessToken);
//需要提交的JSON数据
String outputStr = "{\"expire_seconds\": %d,\"action_name\": \"QR_SCENE\", \"action_info\": {\"scene\": {\"scene_id\":%d}}}";
//创建临时带参数二维码
JSONObject jsonObject = httpRequest(requestUrl, "POST", String.format(outputStr, expireSeconds, sceneId));
if(null!=jsonObject){
try {
weChatQRCode = new WeChatQRCode();
weChatQRCode.setTicket(jsonObject.getString("ticket"));
weChatQRCode.setExpire_seconds(jsonObject.getInt("expire_seconds"));
logger.info("创建临时带参二维码成功,ticket="+weChatQRCode.getTicket()+",expire_seconds="+weChatQRCode.getExpire_seconds());
} catch (Exception e) {
weChatQRCode = null;
String errorMsg = jsonObject.getString("errmsg");
logger.info("创建临时带参二维码失败,错误码是="+errorMsg);
}
}
return weChatQRCode;
}
/**
* 创建永久二维码
* @param accessToken
* @param sceneId 场景Id
* @param sceneStr 场景IdsceneStr
* @return
*/
//数字ID用这个{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": 123}}}
//或者也可以使用以下POST数据创建字符串形式的二维码参数:
//字符ID用这个{"action_name": "QR_LIMIT_STR_SCENE", "action_info": {"scene": {"scene_str": "hfrunffgha"}}}
public static String createPermanentQRCode(String accessToken, String sceneStr) {
String ticket = null;
String requestUrl = WxConstants.QR_CODE_URL.replace("TOKEN", accessToken);
String outputStr = "{\"action_name\": \"QR_LIMIT_STR_SCENE\", \"action_info\":{\"scene\": {\"scene_str\":%s}}}";
JSONObject jsonObject = httpRequest(requestUrl, "POST", String.format(outputStr, sceneStr));
if(null!=jsonObject){
try {
ticket = jsonObject.getString("ticket");
logger.info("创建永久带参二维码成功,ticket="+ticket);
} catch (Exception e) {
String errorCode = jsonObject.getString("errcode");
logger.info("创建永久带参二维码失败,错误码是="+errorCode);
String errorMsg = jsonObject.getString("errmsg");
logger.info("创建永久带参二维码失败,错误信息是="+errorMsg);
}
}
return ticket;
}
3.用tiket换取二维码 https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=TICKE
(替换获取到的ticket,放入到浏览器打开即可,若想下载到本地,请参考http://blog.csdn.net/sinat_28505133/article/details/54669167)