问题描述
- 微信 网页授权,第一次能拿到用户信息,之后都拿不到
-
报错:
{"errcode":40029,"errmsg":"req id: L3U1QA0347ns40, invalid code"}java代码
public class WeChatUserInfoController {
private Log logger = LogFactory.getLog(WeChatUserInfoController.class);private String get_access_token_url="https://api.weixin.qq.com/sns/oauth2/access_token?" + "appid=APPID" + "&secret=SECRET" + "&code=CODE&grant_type=authorization_code"; private String get_userinfo="https://api.weixin.qq.com/sns/userinfo?" + "access_token=ACCESS_TOKEN" + "&openid=OPENID" + "&lang=zh_CN"; @RequestMapping(value = "/oauth") public String oauth(HttpServletRequest request, HttpServletResponse response) { logger.info("----- /wechat/oauth: 开始 -----"); String code = ServletRequestUtils.getStringParameter(request, "code", null); String state = ServletRequestUtils.getStringParameter(request, "state", null); get_access_token_url = get_access_token_url.replace("APPID", "*****"); get_access_token_url = get_access_token_url.replace("SECRET", "*****"); get_access_token_url = get_access_token_url.replace("CODE", code); WeChatAccessToken at = getAccessToken(get_access_token_url); String access_token = at.getAccess_token(); String openid = at.getOpenid(); logger.info("----- code: 开始 -----" + code); logger.info("----- access_token: 开始 -----" + access_token); logger.info("----- openid: 开始 -----" + openid); logger.info("----- openid: 开始 -----" + at.getExpires_in()); logger.info("----- openid: 开始 -----" + at.getRefresh_token()); logger.info("----- openid: 开始 -----" + at.getScope()); logger.info("----- openid: 开始 -----" + at.getUnionid()); get_userinfo = get_userinfo.replace("ACCESS_TOKEN", access_token); get_userinfo = get_userinfo.replace("OPENID", openid); WeChatUserInfo ui = getUserInfo(get_userinfo); request.setAttribute("userInfo", ui); logger.info("----- /wechat/oauth: 结束 -----"); return "/h5/activity/demo"; } public WeChatAccessToken getAccessToken(String url){ WeChatAccessToken at = HttpClientUtil.simpleHttpCallGet( url, null, null, 20000 , new HttpClientCallBack<WeChatAccessToken>() { @Override public WeChatAccessToken handleResult(ResultBean res) throws IOException { GetMethod getMethod = (GetMethod) res.getValueObject(); WeChatAccessToken accessToken = JSON.parseObject(getMethod.getResponseBody(), WeChatAccessToken.class); return accessToken; } }); return at; } public WeChatUserInfo getUserInfo(String url){ WeChatUserInfo userInfo = HttpClientUtil.simpleHttpCallGet( url, null, null, 20000 , new HttpClientCallBack<WeChatUserInfo>() { @Override public WeChatUserInfo handleResult(ResultBean res) throws IOException { GetMethod getMethod = (GetMethod) res.getValueObject(); WeChatUserInfo userInfo = JSON.parseObject(getMethod.getResponseBody(), WeChatUserInfo.class); return userInfo; } }); return userInfo; }
解决方案
微信公众号网页授权获取用户基本信息
微信网页授权获取用户基本信息
微信平台网页授权获取用户基本信息
解决方案二:
首先引导用户去授权页面:
地址:"https://open.weixin.qq.com/connect/oauth2/authorize?appid=appid&redirect_uri=redirect_uri&response_type=code&scope=SCOPE&state=STATE#wechat_redirect";
用户同意授权后,页面将跳转至 redirect_uri/?code=CODE&state=STATE
[php] view plaincopy在CODE上查看代码片派生到我的代码片
//通过code换取token
$code = $_GET['code'];
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=appid&secret=SECRET&code=$code&grant_type=authorization_code";
$json = file_get_contents($url);
$arr = json_decode($json,true);
$token = $arr['access_token'];
$openid = $arr['openid'];
//拿到token后就可以获取用户基本信息了
$url = "https://api.weixin.qq.com/sns/userinfo?access_token=$token&openid=$openid ";
$json = file_get_contents($url);//获取微信用户基本信息
$arr = json_decode($json,true);
$name = $arr['nickname'];//昵称
$imgURL = $arr['headimgurl'];//头像地址
$sex = $arr['sex'];//性别
$province = $arr['province'];//用户个人资料填写的省份
$city= $arr['city'];//普通用户个人资料填写的城市
$country= $arr['country'];//国家,如中国为CN
时间: 2024-12-27 16:20:46