微信公众平台的开发(第一天)
最近老师安排让我去研究微信公众平台,刚开始接触的时候,真的是一头雾水,啥都不知道,慢慢的才熟悉微信公众平台的开发。首先需要需要通过一个接口获取到access_token,今天我就来分享一下我是怎么获取到access_token的。
创建一个菜单
打开微信公众平台自定义菜单。
跳转
在跳转网页那里输入自己的url,然后点击这个url就会跳转到自己的controller,在controller里处理一些操作。(注意:请求必须是get)
/*
* 客户端进行的消息处理
*
* @param request 请求
* @param response 响应
*/
@RequestMapping(value = "/wx/test", method = RequestMethod.GET)
public void disposeClientMessage(HttpServletRequest request, HttpServletResponse response) throws IOException {
String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
String appid = "自己的appid";
//你的回调页
String redirectUri = "需要输入的重定向网址(域名/get/accessToken)";
url = url.replace("APPID", appid);
url = url.replace("REDIRECT_URI", redirectUri);
response.sendRedirect(url);
}
获取access_token
在这里处理刚刚的重定向(注意:重定向的域名必须要在 公众号设置->功能设置->网页授权域名 里加上自己的域名,否则会报10003错误)
/**
* 获取access_token
*
* @param request 请求
*/
@RequestMapping(value = "/get/accessToken", method = RequestMethod.GET)
public void getAccessToken(HttpServletRequest request) {
//首先获得code,通过code来获得openid
String code = request.getParameter("code");
System.out.println(code);
if (code != null) {
//获取openid和access_token的连接
AccessToken accessToken = WeiXinUtils.getAccessToken();
System.out.println("access_token" + accessToken.getToken());
}
}
然后分享一下我的WeiXinUtils
package com.itap.javaee.tools.wx;
import com.itap.javaee.pojo.wechat.AccessToken;
import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
/*
* @author dap
* @create 2019-01-10 20:13
* @desc 用户获取access_token, 众号调用各接口时都需使用access_token
*/
public class WeiXinUtils {
/**
* 微信公众号的APPID和Appsecret,这个是每个微信公众号都唯一的,以后配置不同的公众号配置这里即可
*/
private static final String APPID = "自己的appid";
private static final String APPSECRET = "自己的appsecret";
/**
* 获取access_token的URL
*/
private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
private static final String OPENID_URL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=APPSECRET&code=CODE&grant_type=authorization_code";
/**
* Get请求,方便到一个url接口来获取结果
*
* @param url 传入一个url
* @return JSONObject
*/
public static JSONObject doGetStr(String url) {
DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
JSONObject jsonObject = null;
try {
HttpResponse response = defaultHttpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
if (entity != null) {
String result = EntityUtils.toString(entity, "UTF-8");
jsonObject = JSONObject.fromObject(result);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return jsonObject;
}
/**
* 获取access_token
*
* @return access_token
*/
public static AccessToken getAccessToken() {
AccessToken accessToken = new AccessToken();
String url = ACCESS_TOKEN_URL.replace("APPID", APPID).replace("APPSECRET", APPSECRET);
JSONObject jsonObject = doGetStr(url);
if (jsonObject != null) {
accessToken.setToken(jsonObject.getString("access_token"));
accessToken.setExpireIn((int) jsonObject.getLong("expires_in"));
}
return accessToken;
}
/**
* 获取Openid
*
* @return 获取Openid
*/
public static String getOpenid(String code) {
String url = OPENID_URL.replace("APPID", APPID).replace("APPSECRET", APPSECRET).replace("CODE",code);
String openid = null;
JSONObject jsonObject = doGetStr(url);
System.out.println(jsonObject.toString());
if (jsonObject != null) {
openid = jsonObject.getString("openid");
}
return openid;
}
}
最后就是我想说一下,使用net.sf.json.JSONObject的时候maven需要加一个