java微信JS-SDK之根据access_token获取jsapi_ticket

import com.alibaba.fastjson.JSONObject;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @Author 鹏
 * @Description: 微信专用工具类
 */
public class WeChatUtils {

    /***
     * appid
     */
    private static final String APPID = "sss";
    /***
     * secret
     */
    private static final String SECRET = "ssss";
    /**
     * 请求方式
     */
    private static final String GET = "GET";
    private static final String POST = "POST";

    /**
     * @Author: 鹏
     * @Description: 获取公众号的accessToken(可以修改代码改成获取用户的accessToken)
     */
    public static JSONObject getAccessToken() throws Exception {
        // 获取用户access_token
//        String path = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
//        path = path.replace("APPID", APPID).replace("SECRET", SECRET).replace("CODE", code);
        // 获取公众号获取用户access_token
        InputStream inputStream = null;
        HttpURLConnection connection = null;
        String message = "";
        try {
            String path = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=SECRET";
            path = path.replace("APPID", APPID).replace("SECRET", SECRET);
            URL url = new URL(path);
            connection = (HttpURLConnection) url.openConnection();

            //设置本次请求的方式 , 默认是GET方式, 参数要求都是大写字母
            connection.setRequestMethod(GET);
            //设置连接超时
            connection.setConnectTimeout(50000);
            //是否打开输入流 , 此方法默认为true
            connection.setDoInput(true);
            //是否打开输出流, 此方法默认为false
            connection.setDoOutput(true);
            connection.setUseCaches(false);
            //表示连接
            connection.connect();
            inputStream = connection.getInputStream();

            int size = inputStream.available();
            byte[] bs = new byte[size];
            inputStream.read(bs);
            message = new String(bs, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            if (inputStream != null) {
                inputStream.close();
            }
            // 关闭远程链接
            if (connection != null) {
                connection.disconnect();
            }
        }
        return JSONObject.parseObject(message);
    }


    /**
     * @Author: 鹏
     * 文档位置:https://mp.weixin.qq.com/wiki?action=doc&id=mp1421141115&t=0.4126568759642024#62
     * 附录1-JS-SDK使用权限签名算法
     * @Description: 使用accessToken获取jsapi_ticket
     */
    public static JSONObject getJsApiTicket(String accessToken) throws Exception {
        InputStream inputStream = null;
        HttpURLConnection connection = null;
        String message = "";
        try {
            String path = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi";
            path = path.replace("ACCESS_TOKEN", accessToken);
            URL url = new URL(path);
            connection = (HttpURLConnection) url.openConnection();

            //设置本次请求的方式 , 默认是GET方式, 参数要求都是大写字母
            connection.setRequestMethod(GET);
            //设置连接超时
            connection.setConnectTimeout(50000);
            //是否打开输入流 , 此方法默认为true
            connection.setDoInput(true);
            //是否打开输出流, 此方法默认为false
            connection.setDoOutput(true);
            connection.setUseCaches(false);
            //表示连接
            connection.connect();
            inputStream = connection.getInputStream();

            int size = inputStream.available();
            byte[] bs = new byte[size];
            inputStream.read(bs);
            message = new String(bs, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            if (inputStream != null) {
                inputStream.close();
            }
            // 关闭远程链接
            if (connection != null) {
                connection.disconnect();
            }
        }
        return JSONObject.parseObject(message);
    }
}

 

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值