微信公众号获取access_token的方法实例分析
本文实例讲述了微信公众号获取access_token的方法。分享给大家供大家参考,具体如下:
上一版需求做了微信公众号开发,秀了一波操作,也遇到了很多坑。现在把微信公众号一些基本的操作记录一下。
微信公众号获取access_token 官方文档地址
access_token是公众号的全局唯一接口调用凭据,我们和微信服务器进行交互,服务器通过access_token判断我们是谁(哪个公众号服务的请求)。所以我们在开发过程中服务端拿到的access_token是一定不能显式暴露给外部,否则将导致数据安全问题。别人拿到你的accessToken操作你的公众号。access_token的有效期目前为2个小时,过期需要再次获取。
下面是一种获取access_token方式
1.项目添加httpclient相关依赖,示例使用httpclient请求微信服务器,获取微信返回结果。
org.apache.httpcomponents httpclient 4.5.3 org.apache.httpcomponents httpcore 4.4.6
2.httpClientUtil类,网上随手找的试了一下本例的doget方法没有问题,其他的暂不考虑
publicclassHttpClientUtil{
publicstaticStringdoGet(Stringurl,Mapparam){
//创建Httpclient对象
CloseableHttpClienthttpclient=HttpClients.createDefault();
StringresultString="";
CloseableHttpResponseresponse=null;
try{
//创建uri
URIBuilderbuilder=newURIBuilder(url);
if(param!=null){
for(Stringkey:param.keySet()){
builder.addParameter(key,param.get(key));
}
}
URIuri=builder.build();
//创建httpGET请求
HttpGethttpGet=newHttpGet(uri);
//执行请求
response=httpclient.execute(httpGet);
//判断返回状态是否为200
if(response.getStatusLine().getStatusCode()==200){
resultString=EntityUtils.toString(response.getEntity(),"UTF-8");
}
}catch(Exceptione){
e.printStackTrace();
}finally{
try{
if(response!=null){
response.close();
}
httpclient.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
returnresultString;
}
publicstaticStringdoGet(Stringurl){
returndoGet(url,null);
}
publicstaticStringdoPost(Stringurl,Mapparam){
//创建Httpclient对象
CloseableHttpClienthttpClient=HttpClients.createDefault();
CloseableHttpResponseresponse=null;
StringresultString="";
try{
//创建HttpPost请求
HttpPosthttpPost=newHttpPost(url);
//创建参数列表
if(param!=null){
ListparamList=newArrayList<>();
for(Stringkey:param.keySet()){
paramList.add(newBasicNameValuePair(key,param.get(key)));
}
//模拟表单
UrlEncodedFormEntityentity=newUrlEncodedFormEntity(paramList,"utf-8");
httpPost.setEntity(entity);
}
//执行http请求
response=httpClient.execute(httpPost);
resultString=EntityUtils.toString(response.getEntity(),"utf-8");
}catch(Exceptione){
e.printStackTrace();
}finally{
try{
response.close();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
returnresultString;
}
publicstaticStringdoPost(Stringurl){
returndoPost(url,null);
}
publicstaticStringdoPostJson(Stringurl,Stringjson){
//创建Httpclient对象
CloseableHttpClienthttpClient=HttpClients.createDefault();
CloseableHttpResponseresponse=null;
StringresultString="";
try{
//创建HttpPost请求
HttpPosthttpPost=newHttpPost(url);
//创建请求内容
StringEntityentity=newStringEntity(json,ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
//执行http请求
response=httpClient.execute(httpPost);
resultString=EntityUtils.toString(response.getEntity(),"utf-8");
}catch(Exceptione){
e.printStackTrace();
}finally{
try{
response.close();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
returnresultString;
}
}
3.第三步就是简单的测试代码了
publicclassWeChatAccessTokenTest{
publicstaticvoidmain(String[]args){
Mapparams=newHashMap<>();
//TODO:2018/11/16*号改成真实appid
params.put("appid","******");
//TODO:2018/11/16*号改成真实secret
params.put("secret","******");
params.put("grant_type","client_credential");
Stringresponse=HttpClientUtil.doGet("https://api.weixin.qq.com/cgi-bin/token",params);
JSONObjectaccessTokenObject=JSONObject.parseObject(response);
StringaccessToken=accessTokenObject.getString("access_token");
Longexpire=accessTokenObject.getLong("expires_in");
System.out.println(accessToken);
}
}
以上就是微信公众号基础却比较重要的获取access_token操作了!
更多关于java算法相关内容感兴趣的读者可查看本站专题:《Java字符与字符串操作技巧总结》、《Java数组操作技巧总结》、《Java数学运算技巧总结》、《Java编码操作技巧总结》和《Java数据结构与算法教程》
希望本文所述对大家java程序设计有所帮助。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。