收藏
回答

小程序登录获取用户手机号,后端鉴权报40001问题,求大神解答?

{"errcode":40001,"errmsg":"invalid credential, access_token is invalid or not latest, could get access_token by getStableAccessToken, more details at https://mmbizurl.cn/s/JtxxFh33r rid: 6790dbd3-3e362538-5e995e6c"}


代码片段:


public MiniAppUserDTO authPhoneNumber(String openID, String code) {

        //checkWxAccessToken();

        String phoneUrl = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=ACCESS_TOKEN";
        JSONObject jsonObject = new JSONObject();
        //checkWxAccessToken();
        jsonObject.put("access_token", getAccessToken());
        jsonObject.put("code", code);
        //jsonObject.put("openid", openID);

        log.info("json -- " + jsonObject.toString());
        String result = okHttpHelper.invokePost(phoneUrl, jsonObject.toString(), 10);
        log.info("result -- " + result);
        JSONObject resultJSON = JSONObject.parseObject(result);

        //userDOMapper.updateByOpenId(openID,)
        MiniAppUserDTO miniAppUserDTO = new MiniAppUserDTO();
        miniAppUserDTO.setPhoneNumber(resultJSON.getString("purePhoneNumber"));

        if (StringUtils.isNotBlank(miniAppUserDTO.getPhoneNumber())) {
            userDOMapper.updateByOpenId(openID, miniAppUserDTO.getPhoneNumber());
        }

        return miniAppUserDTO;
    }


//    GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET


    private String getAccessToken() {
        String url2 = "https://api.weixin.qq.com/cgi-bin/stable_token";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("grant_type", "client_credential");
        jsonObject.put("appid", appid);
        jsonObject.put("secret", appKey);
        jsonObject.put("force_refresh",false);
        String res = okHttpHelper.invokePost(url2, jsonObject.toString(), 10);
        log.info("getAccessToken--- " + res);
        return res;

    }


回答关注问题邀请回答
收藏

1 个回答

登录 后发表内容