微信开发流程梳理(一)

    该经验适用于网页程序的开发,且针对企业号!

    一、获取code

        1、官方url:https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_base&state={2}#wechat_redirect
        2、代码判断:

 String code = request.getParameter("code");
 if (StringUtils.isBlank(code)) {
        String redirectUri = HttpRequest.getAuthorizeCode("appid", "http://***/***, "state");
        response.sendRedirect(redirectUri);
    } else if (StringUtils.isNotBlank(code) && "AUTHORITY".equals(code)) {
    // 真正的业务处理
}
public String getAuthorizeCode(Object... arguments) {
    return MessageFormat.format(redirectURL, arguments);
}

    二、根据code获取用户的userid与deviceid

        1、官方url:https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token={0}&code={1}&agentid={2},agentid=企业号中的应用id

    三、GET请求的代码:

public static String httpsGet(String requestURL, String charset) {
        HttpsURLConnection connection = null;
        try {
            
            TrustManager [] tms = { new MyTrustManager() };
            SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
            sslContext.init(null, tms, new SecureRandom());
            SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
			URL url = new URL(requestURL);
            connection = (HttpsURLConnection) url.openConnection();
            connection.setSSLSocketFactory(sslSocketFactory);
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setUseCaches(false);
            connection.setRequestMethod("GET");
            connection.connect();
            String respStr = getResponseContent(connection.getInputStream(), charset);
            return respStr;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
    }
	
	private static class MyTrustManager implements X509TrustManager {

        @Override
        public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
        }

        @Override
        public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
        }

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
        
    }





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值