微信公众平台当中,接口的调用很重要,既需要调用到微信提供的接口,微信也需要调用你提供的开发者url,接口的调用都是基于请求的,基本上分为POST和GET两种请求,本篇文章将重点介绍测试号的申请与微信接口调用。
首先是申请微信公众平台测试账号,微信公众平台的测试账号功能权限是比较接近已认证服务号的,只不过是因为这是测试账号,所以接口的请求限制次数要明显少于普通账号。
测试账号的申请地址为:http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login
申请的过程也很简单,点击登陆,然后用手机的微信扫一扫功能扫一下二维码就ok ,确认登陆后就会进入到以下界面:

测试账号到此就申请成功了,现在我们可以把我们的开发者url和token填入URL和Token的输入框内,点提交及可完成绑定!测试账号的申请到此结束。
接下来要介绍微信接口的调用,本文主要介绍获取access_token的接口调用,其他接口以此类推。
微信官方文档对access_token的描述为:“access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token。正常情况下access_token有效期为7200秒,重复获取将导致上次获取的access_token失效。由于获取access_token的api调用次数非常有限,建议开发者全局存储与更新access_token,频繁刷新access_token会导致api调用受限,影响自身业务。”
接口调用请求说明
http请求方式: GET 地址:https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET 参数说明
参数 | 是否必须 | 说明 |
---|---|---|
grant_type | 是 | 获取access_token填写client_credential |
appid | 是 | 第三方用户唯一凭证 |
secret | 是 | 第三方用户唯一凭证密钥,即appsecret |
返回说明
正常情况下,微信会返回下述JSON数据包给公众号:
{"access_token":"ACCESS_TOKEN","expires_in":7200}
参数 | 说明 |
---|---|
access_token | 获取到的凭证 |
expires_in | 凭证有效时间,单位:秒 |
根据返回说明中的json数据,我们可以首先封装一个参数接收类,代码如下:
package com.yimik.wechat.wechat.model; public class AccessTokenModel extends ErrorCodeModel{ private String access_token; //获取到的凭证 private String expires_in; //凭证有效时间,单位:秒 public String getAccess_token() { return access_token; } public void setAccess_token(String access_token) { this.access_token = access_token; } public String getExpires_in() { return expires_in; } public void setExpires_in(String expires_in) { this.expires_in = expires_in; } }
然后就是借口的调用了,因为接口走的是http协议,所以可以用第三方jar包HttpClient来处理http请求,在pom.xml中添加下列代码引入httpclient
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.2-beta1</version> </dependency>
外加一个httpclient常用操作的封装类
package com.yimik.wechat.tool; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.ArrayList; import java.util.List; import java.util.Map; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.ParseException; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.utils.URLEncodedUtils; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * 封装了一些采用HttpClient发送HTTP请求的方法 */ public class HttpClientUtil { private static Logger logger = LoggerFactory.getLogger(HttpClientUtil.class); private HttpClientUtil(){} /** * 发送HTTP_GET请求 * @see 该方法会自动关闭连接,释放资源 * @param requestURL 请求地址(含参数) * @param decodeCharset 解码字符集,解析响应数据时用之,其为null时默认采用UTF-8解码 * @return 远程主机响应正文 */ public static String sendGetRequest(String reqURL, String decodeCharset){ long responseLength = 0; //响应长度 String responseContent = null; //响应内容 HttpClient httpClient = new DefaultHttpClient(); //创建默认的httpClient实例 HttpGet httpGet = new HttpGet(reqURL); //创建org.apache.http.client.methods.HttpGet try{ HttpResponse response = httpClient.execute(httpGet); //执行GET请求 HttpEntity entity = response.getEntity(); //获取响应实体 if(null != entity){ responseLength = entity.getContentLength(); responseContent = EntityUtils.toString(entity, decodeCharset==null ? "UTF-8" : decodeCharset); EntityUtils.consume(entity); //Consume response content } System.out.println("请求地址: " + httpGet.getURI()); System.out.println("响应状态: " + response.getStatusLine()); System.out.println("响应长度: " + responseLength); System.out.println("响应内容: " + responseContent); }catch(ClientProtocolException e){ logger.error("该异常通常是协议错误导致,比如构造HttpGet对象时传入的协议不对(将'http'写成'htp')或者服务器端返回的内容不符合HTTP协议要求等,堆栈信息如下", e); }catch(ParseException e){ logger.error(e.getMessage(), e); }catch(IOException e){ logger.error("该异常通常是网络原因引起的,如HTTP服务器未启动等,堆栈信息如下", e); }finally{ httpClient.getConnectionManager().shutdown(); //关闭连接,释放资源 } return responseContent; } /** * 发送HTTP_POST请求 * @see 该方法为<code>sendPostRequest(String,String,boolean,String,String)</code>的简化方法 * @see 该方法在对请求数据的编码和响应数据的解码时,所采用的字符集均为UTF-8 * @see 当<code>isEncoder=true</code>时,其会自动对<code>sendData</code>中的[中文][|][ ]等特殊字符进行<code>URLEncoder.encode(string,"UTF-8")</code> * @param isEncoder 用于指明请求数据是否需要UTF-8编码,true为需要 */ public static String sendPostRequest(String reqURL, String sendData, boolean isEncoder){ return sendPostRequest(reqURL, sendData, isEncoder, null, null); } /** * 发送HTTP_POST请求 * @see 该方法会自动关闭连接,释放资源 * @see 当<code>isEncoder=true</code>时,其会自动对<code>sendData</code>中的[中文][|][ ]等特殊字符进行<code>URLEncoder.encode(string,encodeCharset)</code> * @param reqURL 请求地址 * @param sendData 请求参数,若有多个参数则应拼接成param11=value11?m22=value22?m33=value33的形式后,传入该参数中 * @param isEncoder 请求数据是否需要encodeCharset编码,true为需要 * @param encodeCharset 编码字符集,编码请求数据时用之,其为null时默认采用UTF-8解码 * @param decodeCharset 解码字符集,解析响应数据时用之,其为null时默认采用UTF-8解码 * @return 远程主机响应正文 */ public static String sendPostRequest(String reqURL, String sendData, boolean isEncoder, String encodeCharset, String decodeCharset){ String responseContent = null; HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(reqURL); //httpPost.setHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded; charset=UTF-8"); httpPost.setHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded"); try{ if(isEncoder){ List<NameValuePair> formParams = new ArrayList<NameValuePair>(); for(String str : sendData.split("&")){ formParams.add(new BasicNameValuePair(str.substring(0,str.indexOf("=")), str.substring(str.indexOf("=")+1))); } httpPost.setEntity(new StringEntity(URLEncodedUtils.format(formParams, encodeCharset==null ? "UTF-8" : encodeCharset))); }else{ httpPost.setEntity(new StringEntity(sendData)); } HttpResponse response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); if (null != entity) { responseContent = EntityUtils.toString(entity, decodeCharset==null ? "UTF-8" : decodeCharset); EntityUtils.consume(entity); } }catch(Exception e){ logger.error("与[" + reqURL + "]通信过程中发生异常,堆栈信息如下", e); }finally{ httpClient.getConnectionManager().shutdown(); } return responseContent; } /** * 发送HTTP_POST请求 * @see 该方法会自动关闭连接,释放资源 * @see 该方法会自动对<code>params</code>中的[中文][|][ ]等特殊字符进行<code>URLEncoder.encode(string,encodeCharset)</code> * @param reqURL 请求地址 * @param params 请求参数 * @param encodeCharset 编码字符集,编码请求数据时用之,其为null时默认采用UTF-8解码 * @param decodeCharset 解码字符集,解析响应数据时用之,其为null时默认采用UTF-8解码 * @return 远程主机响应正文 */ public static String sendPostRequest(String reqURL, Map<String, String> params, String encodeCharset, String decodeCharset){ String responseContent = null; HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(reqURL); List<NameValuePair> formParams = new ArrayList<NameValuePair>(); //创建参数队列 for(Map.Entry<String,String> entry : params.entrySet()){ formParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } try{ httpPost.setEntity(new UrlEncodedFormEntity(formParams, encodeCharset==null ? "UTF-8" : encodeCharset)); HttpResponse response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); if (null != entity) { responseContent = EntityUtils.toString(entity, decodeCharset==null ? "UTF-8" : decodeCharset); EntityUtils.consume(entity); } }catch(Exception e){ logger.error("与[" + reqURL + "]通信过程中发生异常,堆栈信息如下", e); }finally{ httpClient.getConnectionManager().shutdown(); } return responseContent; } /** * 发送HTTPS_POST请求 * @see 该方法为<code>sendPostSSLRequest(String,Map<String,String>,String,String)</code>方法的简化方法 * @see 该方法在对请求数据的编码和响应数据的解码时,所采用的字符集均为UTF-8 * @see 该方法会自动对<code>params</code>中的[中文][|][ ]等特殊字符进行<code>URLEncoder.encode(string,"UTF-8")</code> */ public static String sendPostSSLRequest(String reqURL, Map<String, String> params){ return sendPostSSLRequest(reqURL, params, null, null); } /** * 发送HTTPS_POST请求 * @see 该方法会自动关闭连接,释放资源 * @see 该方法会自动对<code>params</code>中的[中文][|][ ]等特殊字符进行<code>URLEncoder.encode(string,encodeCharset)</code> * @param reqURL 请求地址 * @param params 请求参数 * @param encodeCharset 编码字符集,编码请求数据时用之,其为null时默认采用UTF-8解码 * @param decodeCharset 解码字符集,解析响应数据时用之,其为null时默认采用UTF-8解码 * @return 远程主机响应正文 */ public static String sendPostSSLRequest(String reqURL, Map<String, String> params, String encodeCharset, String decodeCharset){ String responseContent = ""; HttpClient httpClient = new DefaultHttpClient(); X509TrustManager xtm = new X509TrustManager(){ public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {} public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {} public X509Certificate[] getAcceptedIssuers() {return null;} }; try { SSLContext ctx = SSLContext.getInstance("TLS"); ctx.init(null, new TrustManager[]{xtm}, null); SSLSocketFactory socketFactory = new SSLSocketFactory(ctx); httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", 443, socketFactory)); HttpPost httpPost = new HttpPost(reqURL); List<NameValuePair> formParams = new ArrayList<NameValuePair>(); for(Map.Entry<String,String> entry : params.entrySet()){ formParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } httpPost.setEntity(new UrlEncodedFormEntity(formParams, encodeCharset==null ? "UTF-8" : encodeCharset)); HttpResponse response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); if (null != entity) { responseContent = EntityUtils.toString(entity, decodeCharset==null ? "UTF-8" : decodeCharset); EntityUtils.consume(entity); } } catch (Exception e) { logger.error("与[" + reqURL + "]通信过程中发生异常,堆栈信息为", e); } finally { httpClient.getConnectionManager().shutdown(); } return responseContent; } /** * 发送HTTPS_POST,类型为JSON的请求 * @see 该方法为<code>sendPostSSLRequest(String,String,String,String)</code>方法的简化方法 * @see 该方法在对请求数据的编码和响应数据的解码时,所采用的字符集均为UTF-8 * @see 该方法会自动对<code>params</code>中的[中文][|][ ]等特殊字符进行<code>URLEncoder.encode(string,"UTF-8")</code> */ public static String sendPostSSLRequest(String reqURL, String params){ return sendPostSSLRequest(reqURL, params, null, null); } /** * 发送HTTPS_POST,类型为JSON的请求 * @see 该方法会自动关闭连接,释放资源 * @see 该方法会自动对<code>params</code>中的[中文][|][ ]等特殊字符进行<code>URLEncoder.encode(string,encodeCharset)</code> * @param reqURL 请求地址 * @param params 请求参数 * @param encodeCharset 编码字符集,编码请求数据时用之,其为null时默认采用UTF-8解码 * @param decodeCharset 解码字符集,解析响应数据时用之,其为null时默认采用UTF-8解码 * @return 远程主机响应正文 */ public static String sendPostSSLRequest(String reqURL, String params, String encodeCharset, String decodeCharset){ String responseContent = ""; HttpClient httpClient = new DefaultHttpClient(); X509TrustManager xtm = new X509TrustManager(){ public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {} public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {} public X509Certificate[] getAcceptedIssuers() {return null;} }; try { SSLContext ctx = SSLContext.getInstance("TLS"); ctx.init(null, new TrustManager[]{xtm}, null); SSLSocketFactory socketFactory = new SSLSocketFactory(ctx); httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", 443, socketFactory)); HttpPost httpPost = new HttpPost(reqURL); /*List<NameValuePair> formParams = new ArrayList<NameValuePair>(); for(Map.Entry<String,String> entry : params.entrySet()){ formParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); }*/ httpPost.setEntity(new StringEntity(params,encodeCharset==null ? "UTF-8" : encodeCharset)); httpPost.addHeader("content-type", "application/json"); HttpResponse response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); if (null != entity) { responseContent = EntityUtils.toString(entity, decodeCharset==null ? "UTF-8" : decodeCharset); EntityUtils.consume(entity); } } catch (Exception e) { logger.error("与[" + reqURL + "]通信过程中发生异常,堆栈信息为", e); } finally { httpClient.getConnectionManager().shutdown(); } return responseContent; } /** * 发送HTTPS_GET请求 * @see 该方法会自动关闭连接,释放资源 * @param requestURL 请求地址(含参数) * @param decodeCharset 解码字符集,解析响应数据时用之,其为null时默认采用UTF-8解码 * @return 远程主机响应正文 */ public static String sendGetSSLRequest(String reqURL, String decodeCharset){ long responseLength = 0; //响应长度 String responseContent = null; //响应内容 HttpClient httpClient = new DefaultHttpClient(); //创建默认的httpClient实例 X509TrustManager xtm = new X509TrustManager(){ public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {} public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {} public X509Certificate[] getAcceptedIssuers() {return null;} }; try{ SSLContext ctx = SSLContext.getInstance("TLS"); ctx.init(null, new TrustManager[]{xtm}, null); SSLSocketFactory socketFactory = new SSLSocketFactory(ctx); httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", 443, socketFactory)); HttpGet httpGet = new HttpGet(reqURL); //创建org.apache.http.client.methods.HttpGet HttpResponse response = httpClient.execute(httpGet); //执行GET请求 HttpEntity entity = response.getEntity(); //获取响应实体 if(null != entity){ responseLength = entity.getContentLength(); responseContent = EntityUtils.toString(entity, decodeCharset==null ? "UTF-8" : decodeCharset); EntityUtils.consume(entity); //Consume response content } System.out.println("请求地址: " + httpGet.getURI()); System.out.println("响应状态: " + response.getStatusLine()); System.out.println("响应长度: " + responseLength); System.out.println("响应内容: " + responseContent); } catch (Exception e) { logger.error("与[" + reqURL + "]通信过程中发生异常,堆栈信息为", e); }finally{ httpClient.getConnectionManager().shutdown(); //关闭连接,释放资源 } return responseContent; } /** * 发送HTTP_POST请求 * @see 若发送的<code>params</code>中含有中文,记得按照双方约定的字符集将中文<code>URLEncoder.encode(string,encodeCharset)</code> * @see 本方法默认的连接超时时间为30秒,默认的读取超时时间为30秒 * @param reqURL 请求地址 * @param params 发送到远程主机的正文数据,其数据类型为<code>java.util.Map<String, String></code> * @return 远程主机响应正文`HTTP状态码,如<code>"SUCCESS`200"</code><br>若通信过程中发生异常则返回"Failed`HTTP状态码",如<code>"Failed`500"</code> */ public static String sendPostRequestByJava(String reqURL, Map<String, String> params){ StringBuilder sendData = new StringBuilder(); for(Map.Entry<String, String> entry : params.entrySet()){ sendData.append(entry.getKey()).append("=").append(entry.getValue()).append("&"); } if(sendData.length() > 0){ sendData.setLength(sendData.length() - 1); //删除最后一个&符号 } return sendPostRequestByJava(reqURL, sendData.toString()); } /** * 发送HTTP_POST请求 * @see 若发送的<code>sendData</code>中含有中文,记得按照双方约定的字符集将中文<code>URLEncoder.encode(string,encodeCharset)</code> * @see 本方法默认的连接超时时间为30秒,默认的读取超时时间为30秒 * @param reqURL 请求地址 * @param sendData 发送到远程主机的正文数据 * @return 远程主机响应正文`HTTP状态码,如<code>"SUCCESS`200"</code><br>若通信过程中发生异常则返回"Failed`HTTP状态码",如<code>"Failed`500"</code> */ public static String sendPostRequestByJava(String reqURL, String sendData){ HttpURLConnection httpURLConnection = null; OutputStream out = null; //写 InputStream in = null; //读 int httpStatusCode = 0; //远程主机响应的HTTP状态码 try{ URL sendUrl = new URL(reqURL); httpURLConnection = (HttpURLConnection)sendUrl.openConnection(); httpURLConnection.setRequestMethod("POST"); httpURLConnection.setDoOutput(true); //指示应用程序要将数据写入URL连接,其值默认为false httpURLConnection.setUseCaches(false); httpURLConnection.setConnectTimeout(30000); //30秒连接超时 httpURLConnection.setReadTimeout(30000); //30秒读取超时 out = httpURLConnection.getOutputStream(); out.write(sendData.toString().getBytes()); //清空缓冲区,发送数据 out.flush(); //获取HTTP状态码 httpStatusCode = httpURLConnection.getResponseCode(); //该方法只能获取到[HTTP/1.0 200 OK]中的[OK] //若对方响应的正文放在了返回报文的最后一行,则该方法获取不到正文,而只能获取到[OK],稍显遗憾 //respData = httpURLConnection.getResponseMessage(); // //处理返回结果 // BufferedReader br = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream())); // String row = null; // String respData = ""; // if((row=br.readLine()) != null){ //readLine()方法在读到换行[\n]或回车[\r]时,即认为该行已终止 // respData = row; //HTTP协议POST方式的最后一行数据为正文数据 // } // br.close(); in = httpURLConnection.getInputStream(); byte[] byteDatas = new byte[in.available()]; in.read(byteDatas); return new String(byteDatas) + "`" + httpStatusCode; }catch(Exception e){ logger.error(e.getMessage()); return "Failed`" + httpStatusCode; }finally{ if(out != null){ try{ out.close(); }catch (Exception e){ logger.error("关闭输出流时发生异常,堆栈信息如下", e); } } if(in != null){ try{ in.close(); }catch(Exception e){ logger.error("关闭输入流时发生异常,堆栈信息如下", e); } } if(httpURLConnection != null){ httpURLConnection.disconnect(); httpURLConnection = null; } } } }
然后就可以写测试类来获取access_token了,在TokenService中加入方法
/** * 获取全局返回码 * * @param appid 微信appid * @param secret 微信secret * @return * @throws Exception */ public String getAccessToken(String appid,String secret){ if(StringUtils.isEmpty(appid)||StringUtils.isEmpty(secret)) return "请输入appid或appsecret"; String accessToken = ""; String accessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token"; AccessTokenModel accessTokenModel = JsonMapper.buildNormalMapper().fromJson(HttpClientUtil.sendGetSSLRequest(accessTokenUrl+"?grant_type=client_credential&appid="+appid+"&secret="+secret, null), AccessTokenModel.class); if(StringUtils.isEmpty(accessTokenModel.getAccess_token())){ return null; }else{ accessToken=accessTokenModel.getAccess_token(); } return accessToken; }
测试项目代码将会在文章末尾发布链接以供下载。部署项目完成后启动服务器,打开地址:http://127.0.0.1/wechat/testAccessToken.jsp,调用成功后得到以下界面如下:

弹出的内容即为微信access_token,至此,微信接口调用测试成功。
项目已打包上传到百度网盘,链接:http://pan.baidu.com/s/1rm4u6
pom.xml是什么东西?上网看了下,用的Maven?我没用maven,怎么弄?springMVC+hibernate,服务器resin,没有svn
这个比较老了,新接口的demo一直没有时间写,这个demo的话用的是maven,可以下载一个maven,用法很简单的,网上很多相关资料