接到这个项目的时候,很迷茫不知道如何下手,刚入门,把自己最近学习的总结一下:
(注册申请认证流程这里我就忽略)梳理下过程,当微信用户给你的微信公众号发送消息后,消息到达微信服务器被处理成XML数据包并转发给开发者服务器(URL),开发者服务器接收到数据包后就会把用户消息经过一系列的逻辑处理并再转送给微信服务器,最后微信服务器再推送给用户。
数据交互的流程:用户发送消息—到达微信服务器—转发给开发者服务器—转送微信服务器—推送给用户;
在了解并查看开发技术文档之后,以下就是要做第一步获取access_token;
首先要会创建的是一个web项目,环境搭好启动ok了。如下是代码:
结构图:下载okhttp相关的包

package com.***.gzhkf.web; import com.***.gzhkf.util.OkHttpUtil; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import java.util.HashMap; import java.util.Map; /** * Created by ** on 2017/11/8. */ @Controller @RequestMapping public class TokenController { private static String appid = "你的appid,在微信公众号后台获取"; private static String secret = "你的秘钥,在微信公众号后台获取"; private static String grant_type = "client_credential"; private static String tokenurl = "https://api.weixin.qq.com/cgi-bin/token"; //http请求方式: GET // https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET @RequestMapping(value = "/wx/token", method = RequestMethod.GET) @ResponseBody public String getToken() { Map param = new HashMap(); param.put("grant_type", grant_type); param.put("appid", appid); param.put("secret", secret); StringBuilder sb = new StringBuilder(); int keylenght = 0; for (Object key : param.keySet()) { if (keylenght < 2) { sb.append(key).append("=").append(param.get(key)).append("&"); } else { sb.append(key).append("=").append(param.get(key)); } keylenght++; } String url=tokenurl+"?"+sb.toString(); System.out.print(url); String response = OkHttpUtil.doGet(url); System.out.println(response); return response; } }

package com.**.gzhkf.util; import okhttp3.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.util.concurrent.TimeUnit; /** * Created by **l on 2017/11/8. */ public class OkHttpUtil { private static Logger logger = LoggerFactory.getLogger(OkHttpUtil.class); private static OkHttpClient client = null; // public static final MediaType FORM_URLENCODED // = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8"); public static String doGet(String url) { OkHttpClient client = getClient(); Request request = new Request.Builder().url(url) .get() .build(); Response response = null; try { response = client.newCall(request).execute(); if (response.isSuccessful()) { ResponseBody responseBody = response.body(); if (responseBody != null) { return responseBody.string(); } } } catch (IOException e) { logger.warn(e.getMessage(), "网络异常"); } finally { if (response != null) { response.close(); } } return ""; } public static OkHttpClient getClient() { if (client == null) { client = new OkHttpClient.Builder() .retryOnConnectionFailure(true) .connectTimeout(12, TimeUnit.SECONDS) .readTimeout(12, TimeUnit.SECONDS) .build(); } return client; } }
启动项目:页面输入:http://localhost:端口号/wx/token
正常情况下,微信会返回下述JSON数据包给公众号:
{"access_token":"ACCESS_TOKEN","expires_in":7200} |
完事开头难,希望有帮助