按照微信开放文档,服务端拿到小程序发送的code,去微信服务器换取openid
获取accessToken
package com.mon.utils;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import java.util.HashMap;import java.util.Map;/*** @Description TODO* @Author Yu* @Date -11-26*/public class WxUtils {public static JSONObject getSessionKeyOrOpenId(String code) {String requestUrl = "https://api./sns/jscode2session";Map<String, String> requestUrlParam = new HashMap<>();//小程序appIdrequestUrlParam.put("appid", "APPID");//小程序secretrequestUrlParam.put("secret", "SECRET");//小程序端返回的coderequestUrlParam.put("js_code", code);//默认参数requestUrlParam.put("grant_type", "authorization_code");//发送post请求读取调用微信接口获取openid用户唯一标识JSONObject jsonObject = JSON.parseObject(HttpClientUtils.doGet(requestUrl, requestUrlParam));return jsonObject;}public static JSONObject getAccessToken(){String requestUrl = "https://api./cgi-bin/token";Map<String, String> requestUrlParam = new HashMap<>();//小程序appIdrequestUrlParam.put("appid", "APPID");//小程序secretrequestUrlParam.put("secret", "SECRET");//默认参数requestUrlParam.put("grant_type", "client_credential");JSONObject jsonObject = JSON.parseObject(HttpClientUtils.doGet(requestUrl, requestUrlParam));return jsonObject;}}
HttpClientUtils见HttpClientUtils工具类