分享

微信开发笔记之

 IT技术武馆 2015-12-11

用户首次登陆,实现绑定,以后使用微信账号登陆,好吧,这绑定也是只有绑定,没有解绑,而且是偷偷做的。。。。。、
用到接口 通过code获取用户信息通过openId和 acces_token 获取用户信息
实现步骤:
一 获取code
二 通过code 走微信接口 获取 openId
三 openId 与用户Id 绑定

那么问题来了,code 怎么获取到?
很简单,把request redirect 到微信接口,接口中指定微信回调Url,Url中就会包含code。
说的不够详细,贴出官方文档。
http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html

拿到openId 即可与 userId 绑定
下面贴出部分代码

public String getOpenId(String code) throws Exception{

        OpenId openId  = new OpenId();
        String resultXml = "";
        log.info("URLTOKEN:"+URLTOKEN);
        log.info("APPID:"+APPID);
        log.info("APPSECRET:"+APPSECRET);
        String url = String.format(URLTOKEN, APPID,APPSECRET,code);
        log.info("code:"+code);
        resultXml = HttpClientUtil.doGet(url, URL_CHARSET);
        log.info("getOpenId返回信息:"+resultXml);

        openId = (OpenId) JSONObject.toBean(JSONObject.fromObject(resultXml), OpenId.class);

        return openId.getOpenid();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

openId对象

public class OpenId implements Serializable {

    private static final long serialVersionUID = 1L;


    private String errcode;//错误编码
    private String errmsg; //错误信息

    private String access_token;//网页授权接口调用凭证,注意:此access_token与基础支持的access_token不同
    private String expires_in;//access_token接口调用凭证超时时间,单位(秒)
    private String refresh_token;//用户刷新access_token
    private String openid;//用户唯一标识,请注意,在未关注公众号时,用户访问公众号的网页,也会产生一个用户和公众号唯一的OpenID
    private String scope;//用户授权的作用域,使用逗号(,)分隔
    private String unionid;//只有在用户将公众号绑定到微信开放平台账号后,才会出现该字段

    public String getAccess_token() {
        return access_token;
    }
    public void setAccess_token(String access_token) {
        this.access_token = access_token;
    }
    public String getExpires_in() {
        return expires_in;
    }
    public void setExpires_in(String expires_in) {
        this.expires_in = expires_in;
    }
    public String getRefresh_token() {
        return refresh_token;
    }
    public void setRefresh_token(String refresh_token) {
        this.refresh_token = refresh_token;
    }
    public String getOpenid() {
        return openid;
    }
    public void setOpenid(String openid) {
        this.openid = openid;
    }
    public String getScope() {
        return scope;
    }
    public void setScope(String scope) {
        this.scope = scope;
    }
    public String getUnionid() {
        return unionid;
    }
    public void setUnionid(String unionid) {
        this.unionid = unionid;
    }
    public String getErrcode() {
        return errcode;
    }
    public void setErrcode(String errcode) {
        this.errcode = errcode;
    }
    public String getErrmsg() {
        return errmsg;
    }
    public void setErrmsg(String errmsg) {
        this.errmsg = errmsg;
    }





}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70

HttpClientUtil 工具类

public class HttpClientUtil {
    private static Logger log = Logger.getLogger(HttpClientUtil.class);  
    /**
       httpClient的get请求方式2
     * @return
     * @throws Exception
     */
    public static String doGet(String url, String charset)  throws Exception {
        /*
         * 使用 GetMethod 来访问一个 URL 对应的网页,实现步骤: 1:生成一个 HttpClinet 对象并设置相应的参数。
         * 2:生成一个 GetMethod 对象并设置响应的参数。 3:用 HttpClinet 生成的对象来执行 GetMethod 生成的Get
         * 方法。 4:处理响应状态码。 5:若响应正常,处理 HTTP 响应内容。 6:释放连接。
         */
        /* 1 生成 HttpClinet 对象并设置参数 */
        HttpClient httpClient = new HttpClient();
        // 设置 Http 连接超时为5秒
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
        /* 2 生成 GetMethod 对象并设置参数 */
        GetMethod getMethod = new GetMethod(url);
        // 设置 get 请求超时为 5 秒
        getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 5000);
        // 设置请求重试处理,用的是默认的重试处理:请求三次
        getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,  new DefaultHttpMethodRetryHandler());
        String response = "";
        /* 3 执行 HTTP GET 请求 */
        try {
            int statusCode = httpClient.executeMethod(getMethod);
            /* 4 判断访问的状态码 */
            if (statusCode != HttpStatus.SC_OK) {
                log.error("请求出错: "+ getMethod.getStatusLine());
            }
            /* 5 处理 HTTP 响应内容 */
            // HTTP响应头部信息,这里简单打印
            /*Header[] headers = getMethod.getResponseHeaders();
            for (Header h : headers)
                System.out.println(h.getName() + "------------ " + h.getValue());*/
            // 读取 HTTP 响应内容,这里简单打印网页内容
            byte[] responseBody = getMethod.getResponseBody();// 读取为字节数组
            response = new String(responseBody, charset);
            //System.out.println("----------response:" + response);
            // 读取为 InputStream,在网页内容数据量大时候推荐使用
            // InputStream response = getMethod.getResponseBodyAsStream();
        } catch (HttpException e) {
            // 发生致命的异常,可能是协议不对或者返回的内容有问题
            log.error("请检查输入的URL!");
        } catch (IOException e) {
            // 发生网络异常
            log.error("发生网络异常!");
        } finally {
            /* 6 .释放连接 */
            getMethod.releaseConnection();
        }
        return response;
    }

    public static String doPost(String url,String body,String charset){  
        HttpClient client = new HttpClient();     
        //设置代理服务器地址和端口       
        //client.getHostConfiguration().setProxy("proxy_host_addr",proxy_port);  
        //使用GET方法,如果服务器需要通过HTTPS连接,那只需要将下面URL中的http换成https  
        // HttpMethod method = new GetMethod("http://10.1.14.20:8088/workflowController/service/todo/addTask");   
        //使用POST方法  
        PostMethod method = new PostMethod(url);   

        ((PostMethod) method).setRequestBody(body);

        HttpMethodParams param = method.getParams();  
        param.setContentCharset(charset);  
        String str = null;
        try {
            int statusCode = client.executeMethod(method);
            if (statusCode != HttpStatus.SC_OK) {
                log.error("请求出错: "+ method.getStatusLine());
            }
            str = method.getResponseBodyAsString();
        } catch (HttpException e) {
            log.error("请检查输入的URL!");
        } catch (IOException e) {
            log.error("发生网络异常!");
        }finally{
            method.releaseConnection();  
        }
        log.info(str.toString());
        //释放连接  
        return str;

    }  
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多