微信公众号开发中token和access_token有何区别?
- 内容介绍
- 文章标签
- 相关推荐
本文共计335个文字,预计阅读时间需要2分钟。
1. 获取基本支持的access_token官方文档:[点击访问](https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_access_token.) 实现示例:在CommonConstant.java中定义 java String WXGZH_TOKEN_URL=https://api.weixin.qq.com/cgi-bin/token?grant_type=;
1. 基础支持的access_token
官方文档:
实现:
CommonConstant.java:
String WXGZH_TOKEN_URL = "api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s"; String WXGZH_SNS_OAUTH2_ACCESS_TOKEN_URL = "api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code"; public JsonResponse getToken(String appid,String secret){ String token = (String)wxgzhCommonCache.getBaseToken(appid); if(StringUtils.isEmpty(token)){ String url = String.format(CommonConstant.WXGZH_TOKEN_URL,appid,secret); String result = okHttpUtil.get(url); if(result==null){ logger.info("result is null"); return result(-1,"获取token失败",null); } JSONObject resultObj = JSON.parseObject(result); if(resultObj==null){ logger.info("result is null"); return result(-1,"获取token失败",null); } String errcode = resultObj.getString("errcode"); if(StringUtils.isNotEmpty(errcode)){ logger.info("获取token,返回错误,result={}",result); return result(-2,"获取token,返回错误",resultObj); } token = resultObj.getString("access_token"); int expires_in = resultObj.getInteger("expires_in"); expires_in = expires_in-300;//预留5分钟 wxgzhCommonCache.putBaseToken(appid,token,expires_in); } return result(0,"",token); }2. 网页授权需要获取的用户access_token
官方文档:
developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html
代码实现:
本文共计335个文字,预计阅读时间需要2分钟。
1. 获取基本支持的access_token官方文档:[点击访问](https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_access_token.) 实现示例:在CommonConstant.java中定义 java String WXGZH_TOKEN_URL=https://api.weixin.qq.com/cgi-bin/token?grant_type=;
1. 基础支持的access_token
官方文档:
实现:
CommonConstant.java:
String WXGZH_TOKEN_URL = "api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s"; String WXGZH_SNS_OAUTH2_ACCESS_TOKEN_URL = "api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code"; public JsonResponse getToken(String appid,String secret){ String token = (String)wxgzhCommonCache.getBaseToken(appid); if(StringUtils.isEmpty(token)){ String url = String.format(CommonConstant.WXGZH_TOKEN_URL,appid,secret); String result = okHttpUtil.get(url); if(result==null){ logger.info("result is null"); return result(-1,"获取token失败",null); } JSONObject resultObj = JSON.parseObject(result); if(resultObj==null){ logger.info("result is null"); return result(-1,"获取token失败",null); } String errcode = resultObj.getString("errcode"); if(StringUtils.isNotEmpty(errcode)){ logger.info("获取token,返回错误,result={}",result); return result(-2,"获取token,返回错误",resultObj); } token = resultObj.getString("access_token"); int expires_in = resultObj.getInteger("expires_in"); expires_in = expires_in-300;//预留5分钟 wxgzhCommonCache.putBaseToken(appid,token,expires_in); } return result(0,"",token); }2. 网页授权需要获取的用户access_token
官方文档:
developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html
代码实现:

