l丶bk 2017-05-10 09:24 采纳率: 60%
浏览 2869
已采纳

java微信公众号开发获取jsapi_ticket问题

public JSONObject index1(Model model,HttpServletRequest request) {
String code=request.getParameter("code");
net.sf.json.JSONObject jsonObject=WeChatUtils.getOpenId(code);//获取微信用户access_token
String openid=jsonObject.getString("openid");
String access_token=jsonObject.getString("access_token");
HttpSession session=request.getSession();
session.setAttribute("access_token",access_token);
session.setAttribute("openid",openid);
System.out.println("index1-- access_token:"+access_token);
// String refresh_token =jsonObject.getString("refresh_token");

    net.sf.json.JSONObject ticket =WeChatUtils.getJsapi_ticket(access_token);

    String jsapi_ticket=jsonObject.getString("ticket");//通过access_token获取jsapi_ticket
    System.out.println("jsapi_ticket:"+jsapi_ticket);


    -------WechatUtil.java
    public static JSONObject getOpenId(String code) {
    String path=OAUTHUSERINFO.replace("APPID", appid).replace("SECRET", appsecret).replace("CODE", code);
    System.out.println("调用微信api url:"+path);
    JSONObject wxjsonObject=HttpRequest(path);
    return wxjsonObject;
}

    private static JSONObject HttpRequest(String requestUrl) {
    JSONObject jsonObject=null;
    StringBuffer buffer=new StringBuffer();
    InputStream inputStream=null;
    try {
        URL url=new URL(requestUrl);    
        HttpsURLConnection httpsURLConnection=(HttpsURLConnection) url.openConnection();
        httpsURLConnection.setDoOutput(true);
        httpsURLConnection.setDoInput(true);
        httpsURLConnection.setUseCaches(false);
        //设置请求方式
        httpsURLConnection.setRequestMethod("GET");
        httpsURLConnection.connect();
        inputStream=httpsURLConnection.getInputStream();
        InputStreamReader inputStreamReader=new InputStreamReader(inputStream,"utf-8");
        BufferedReader bufferedReader=new BufferedReader(inputStreamReader);

        String str=null;
        while((str=bufferedReader.readLine())!=null) {
            buffer.append(str);
        }
        bufferedReader.close();
        inputStreamReader.close();
        //释放资源
        inputStream.close();
        inputStream=null;
        httpsURLConnection.disconnect();
        jsonObject=JSONObject.fromObject(buffer.toString());
        System.out.println(buffer.toString());
    }catch(ConnectException ce) {
        ce.printStackTrace();
        System.out.println("WeChat server connection timed out");
    }catch(Exception e) {
        e.printStackTrace();
        System.out.println("http request error:{}");
    }finally {
        try {
            if(inputStream!=null) {
                inputStream.close();
            }
        }catch(IOException e) {
            e.printStackTrace();
        }
    }
    return jsonObject;
}

初接触微信公众号开发, 前端传个code过来,根据code获取的access_token,获取jsapi_ticket时提示invalid credential, access_token is invalid or not latest hint
access_token是可以获取到的,可是为什么jsapi_ticket获取失败?问题出在什么地方,做的的麻烦给看看

  • 写回答

2条回答 默认 最新

  • 盖文_Gavin 2017-05-11 05:34
    关注

    网页授权access_token和普通access_token的是不同的,你仔细看一下就明白了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格