java获取微信jsapi_ticket方法

public class ShareController extends BaseController  {  
  private static final    String appid = "appid";
    private static final   String secret = "secret ";

//    public static void main(String[] args) {
//        String url = "http://omstest.vmall.com:23568/thirdparty/wechat/vcode/gotoshare?quantity=1&batchName=MATE7";//分享的H5的链接路径
//        Jssdk jssdk = getSign(url);
//        System.out.println(jssdk.getSign()+"FFFF"+jssdk.getNoncestr());
//
//    }
    private String getJsapiTicket(){
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/token?";
        String params = "grant_type=client_credential&appid=" + appid + "&secret=" + secret + "";
        String result = HttpRequestUtils.httpGet(requestUrl+params);
        String access_token = JSONObject.parseObject(result).getString("access_token");
        requestUrl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?";
        params = "access_token=" + access_token + "&type=jsapi";
        result =  HttpRequestUtils.httpGet(requestUrl+params);
        String jsapi_ticket = JSONObject.parseObject(result).getString("ticket");
        int activeTime=Integer.parseInt(JSONObject.parseObject(result).getString("expires_in"));
//        Jssdk jssdk = new Jssdk();
//        jssdk.setActiveTime(activeTime-600);
//        jssdk.setJsapiTicket(jsapi_ticket);
//        jssdkDao.update(jssdk);
        return jsapi_ticket;
    }
    @RequestMapping("/getUrl")
     private  JsonDataVO getSign(String url) {
        String jsapi_ticket = getJsapiTicket();
        String  noncestr =  getNonceStr();
        String timestamp =  getTimeStamp();
        String sign = "jsapi_ticket=" + jsapi_ticket + "&noncestr=" + noncestr + "&timestamp=" + timestamp + "&url=" + url;
        String signature = "";
        try {
            MessageDigest crypt = MessageDigest.getInstance("SHA-1");
            crypt.reset();
            crypt.update(sign.getBytes("UTF-8"));
            signature = byteToHex(crypt.digest());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Jssdk jssdk = new Jssdk();
        jssdk.setAppid(appid);
        jssdk.setNoncestr(noncestr);
        jssdk.setTimestamp(timestamp);
        jssdk.setSign(signature);
        jssdk.setJsapiTicket(jsapi_ticket);
        log.info("signature的值为:"+signature);
        return returnJson(jssdk);

    }

 private String byteToHex(final byte[] hash) {
        Formatter formatter = new Formatter();
        for (byte b : hash) {
            formatter.format("%02x", b);
        }
        String result = formatter.toString();
        formatter.close();
        return result;
    }
   private  String getNonceStr() {
         return UUID.randomUUID().toString().replaceAll("-", "");
    }

    private  String getTimeStamp() {
        return String.valueOf(System.currentTimeMillis() / 1000);

    }
}

 


 

/** * git请求的获得类 * @param url 路径 * @return */
@Slf4j
public class HttpRequestUtils {
    /**
     * 发送get请求
     * @param url    路径
     * @return
     */
    public static String httpGet(String url){
        //get请求返回结果
        String strResult = null;
        try {
            DefaultHttpClient client = new DefaultHttpClient();
            //发送get请求
            HttpGet request = new HttpGet(url);
            HttpResponse response = client.execute(request);

            /**请求发送成功,并得到响应**/
            if (response.getStatusLine().getStatusCode() == org.apache.http.HttpStatus.SC_OK) {
                /**读取服务器返回过来的json字符串数据**/
                strResult = EntityUtils.toString(response.getEntity());
            } else {
                log.error("get请求提交失败:" + url);
            }
        } catch (IOException e) {
            log.error("get请求提交失败:" + url, e);
        }
        return strResult;
    }

}

可参考https://www.cnblogs.com/suoking/p/5341108.html

转自 https://blog.csdn.net/suochunlong/article/details/53032497

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值