爱吃灯笼椒的李阿外 2015-10-15 02:09 采纳率: 0%
浏览 2423

微信 网页授权,第一次能拿到用户信息,之后都拿不到

报错:
{"errcode":40029,"errmsg":"req id: L3U1QA0347ns40, invalid code"}

授权链接
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx26da4c9ef3b45d22&redirect_uri=http%3A%2F%2Fwx.yulin520.com%2Fa2a%2Fwechat%2Foauth&response_type=code&scope=snsapi_userinfo&state=1agj#wechat_redirect

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;
}
  • 写回答

1条回答 默认 最新

  • WorldMobile 2015-10-16 01:04
    关注
     首先引导用户去授权页面:
    地址:"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 
    
    评论

报告相同问题?

悬赏问题

  • ¥50 手写签名不能上传的问题
  • ¥30 linux odbc怎么添加gbase数据库
  • ¥20 电脑开机黑屏,只有一个鼠标,联想zj者y7000
  • ¥20 DXSDK_jun10
  • ¥20 请问这种量表怎么用spss量化分析(作为中介模型的因变量
  • ¥55 AD844 howland电流源如何驱动大额负载
  • ¥15 C++ /QT 内存权限的判断函数列举
  • ¥15 深度学习GFnet理解问题
  • ¥15 单细胞小提琴堆叠图代码
  • ¥50 升级strust2版本到2.3.15.1后使用ognl3.0.6.jar windows环境中没有问题,但部署到linux环境报错