版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zouhuu/article/details/53447250
微信公众号开发文档:https://mp.weixin.qq.com/wiki
微信公众平台接口调试工具:https://mp.weixin.qq.com/debug/cgi-bin/apiinfo
JAVA微信公众号开发框架:Wechat4j
Wechat4j帮助文档:http://www.chengn.com/wechat4j/wiki/jieruzhinan.html
**************************************************************************
*****我们暂时先不用Wechat4j****
(第一部分)
******需结合微信公众号开发文档来看.******
1.填写服务器基本配置:
URL:必须以http://或https://开头,分别支持80端口和443端口。
没有服务器的可以去下一个花生壳 映射一下。
2.检验signature的示例代码:
public static Boolean checkSignature(String signature,String timestamp,String nonce){
String[] strs=new String[] {token,timestamp,nonce};
Arrays.sort(strs);
StringBuffer content=new StringBuffer();
for (int i = 0; i < strs.length; i++) {
content.append(strs[i]);
}
MessageDigest md = null;
String tmpStr = null;
try {
md = MessageDigest.getInstance("SHA-1");
// 将三个参数字符串拼接成一个字符串进行sha1加密
byte[] digest = md.digest(content.toString().getBytes());
tmpStr = byteToStr(digest);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return signature!=null?signature.toUpperCase().equals(tmpStr.toUpperCase()):false;
}
3.依据接口文档实现业务逻辑。
4.获取access_token:
公众号可以使用AppID和AppSecret调用接口来获取access_token
http请求方式: GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
/**
* 获取access_token
* access_token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
* @param appid 凭证
* @param appsecret 密钥
* @return
*/
public static AccessToken getAccessToken(String appid, String appsecret) {
AccessToken accessToken = null;
String requestUrl = access_token_url.replace("APPID", appid).replace("APPSECRET", appsecret);
JSONObject jsonObject = httpRequest(requestUrl, "GET", null);
if (null != jsonObject) {
try {
accessToken = new AccessToken();
accessToken.setAccess_token(jsonObject.getString("access_token"));
accessToken.setExpires_in(jsonObject.getInt("expires_in"));
} catch (JSONException e) {
accessToken = null;
// 获取token失败
log.error("获取token失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg"));
}
}
return accessToken;
}