code2Session bad code 40018
客户端 creator
tt.login 发送code到服务器
服务器java
请求 https://developer.toutiao.com/api/apps/jscode2session 这个接口
一直返回 bad code 40018
确定code没有被多次调用
这是什么问题
@PostMapping("/TTLogin")
public WxdcxsJSONResult TTLogin(@RequestBody(required = true) JSONObject param) throws Exception {
if (param.get("code") ==null || StringUtils.isBlank(param.get("code").toString())){
return WxdcxsJSONResult.errorMsg("平台信息获取错误");
}
String result = "";
String code =param.get("code").toString();
String date = "appid="+appid+"&secret="+secret+"&code="+code;
result=WebApi.getIns().doGetString(URL_STR_TT,date.toString());
JSONObject jsonObject2 = JSONObject.fromObject(result);
Integer errorcode = jsonObject2.getInt("error");
if (errorcode ==0){
}else{
String errmsg = jsonObject2.getString("errmsg");
System.out.println("errmsg:"+errmsg);
}
}
public String doGetString(String url, String paramaters) throws UnsupportedEncodingException {
String result = null;
// 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
// 创建Get请求
HttpGet httpGet = new HttpGet(url+ "?" + paramaters);
// 响应模型
CloseableHttpResponse response = null;
try {
// 配置信息
RequestConfig requestConfig = RequestConfig.custom()
// 设置连接超时时间(单位毫秒)
.setConnectTimeout(5000)
// 设置请求超时时间(单位毫秒)
.setConnectionRequestTimeout(5000)
// socket读写超时时间(单位毫秒)
.setSocketTimeout(5000)
// 设置是否允许重定向(默认为true)
.setRedirectsEnabled(true).build();
// 将上面的配置信息 运用到这个Get请求里
httpGet.setConfig(requestConfig);
// 由客户端执行(发送)Get请求
response = httpClient.execute(httpGet);
HttpResponse response1 = httpClient.execute(httpGet);
int statusCode = response1.getStatusLine().getStatusCode();
if (HttpStatus.SC_OK != statusCode) {
throw new IOException("bad get request ,return code is:" + statusCode);
}
HttpEntity entity = response1.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity, "UTF-8");
}
return result;
} catch (ClientProtocolException e) {
return e.toString();
} catch (IOException e) {
return e.toString();
} finally {
try {
// 释放资源
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}