接到這個項目的時候,很迷茫不知道如何下手,剛入門,把自己最近學習的總結一下:
(注冊申請認證流程這里我就忽略)梳理下過程,當微信用戶給你的微信公眾號發送消息后,消息到達微信服務器被處理成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} |
完事開頭難,希望有幫助
