找到原因了,accesstoken要用小程序的appid和密钥生成,而不是微信公众号的,另外accesstoken带在参数中,content要以json格式放在post请求的body中
public String test(String test) {
String appId = miniProgramAppId
String appSecret = miniProgramSecret
Token token = CommonUtil.getTokenminiProgramAppId, miniProgramSecret);
System.out.println(token.getAccessToken());
JSONObject jsonObject = new JSONObject();
jsonObject.put("content", 志愿兵维权);
String o=""
try
o = okHttpPost("https://api.weixin.qq.com/wxa/msg_sec_check?access_token="+token.getAccessToken(), jsonObject.toString());
} catch (Exception e) {
e.printStackTrace();
}
return o;
public String okHttpPost(String url, String json){
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse"application/json");
okhttp3.RequestBody body = okhttp3.RequestBody.create(json,mediaType);
Request request = new Request.Builder()
.url(url)
.method("POST", body)
.addHeader("Content-Type", "application/json"
.build();
try
Response response = client.newCall(request).execute();
return response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
return null
}
dependency
groupIdcom.squareup.okhttp3</groupId
artifactIdokhttp</artifactId
version4.2.2</version
</dependency