微信公众号开发网页授权获得OPENID的过滤器

2017-05-11  本文已影响0人  等待未果

1.填写授权回调页面域名:

1.1获取微信公众平台测试账号

2授权成功获得Openid

2.1用户同意授权,获取code

在确保微信公众账号拥有授权作用域<scope参数>的权限的前提下(服务号获得高级接口后,默认拥有scope参数中的snsapi_basesnsapi_userinfo),引导关注者打开如下页面:

https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect 
//若提示“该链接无法访问”,请检查参数是否填写错误,是否拥有scope参数对应的授权作用域权限。 

2.1.1封装普通url成授权url

     https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx9240e5de6afdd7b1&redirect_uri=http://zhixiaoyi.nat300.top/weixinOAuth/OAuthServlet.do&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect

2.1.2授权访问获得code

alt  用户进行授权alt 用户进行授权

2.2通过code获得openid

这里通过code换取的是一个特殊的网页授权access_token,与基础支持中的access_token(该access_token用于调用其他接口)不同。公众号可通过下述接口来获取网页授权access_token。如果网页授权的作用域为snsapi_base,则本步骤中获取到网页授权access_token的同时,也获取到了openidsnsapi_base式的网页授权流程即到此为止。

    { "access_token":"ACCESS_TOKEN",    
      "expires_in":7200,    
      "refresh_token":"REFRESH_TOKEN",    
      "openid":"OPENID",    
      "scope":"SCOPE" }     
    public class WeixinUtil {
        // 公众号id
        public static String APPID = "wx9240e5de6afdd7b1";
        // 公众号密钥
        public static String APPSECRET = "2de51d7fae9cb5f36d5468c15bc288fe";
        // 用户同意授权url,获取code
        public static String AUTHORIZE_URL = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect";
        // 通过code换取网页授权access_token的url
        public static String ACCESS_TOKEN_BY_CODE_URL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
        // 授权域名
        public static String DOMAIN_NAME = "http://zhixiaoyi.nat300.top";
        // url范围
        public static String SCOPR = "snsapi_userinfo";
    
        /**
         * Get请求
         * 
         * @param url
         * @return
         */
        public static JSONObject doGetStr(String url) {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpGet httpGet = new HttpGet(url);
            JSONObject jsonObject = null;
            try {
                HttpResponse httpRequest = httpClient.execute(httpGet);
                HttpEntity entity = httpRequest.getEntity();
    
                if (entity != null) {
                    String result = EntityUtils.toString(entity, "UTF-8");
                    jsonObject = JSONObject.fromObject(result);
                }
    
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            return jsonObject;
        }
    
        /**
         * Post请求
         * 
         * @param url
         * @param outStr
         * @return
         */
        public static JSONObject doPostStr(String url, String outStr) {
    
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(url);
            JSONObject jsonObject = null;
            try {
                httpPost.setEntity(new StringEntity(outStr, "UTF-8"));
                HttpResponse httpRequest = httpClient.execute(httpPost);
                HttpEntity entity = httpRequest.getEntity();
    
                String result = EntityUtils.toString(entity, "UTF-8");
                jsonObject = JSONObject.fromObject(result);
    
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            return jsonObject;
        }
    
        /**
         * 网页授权获取openId第2步,根据code取得openId
         * 
         * @param appid
         *            公众号的唯一标识
         * @param secret
         *            公众号的appsecret密钥
         * @param code
         *            code为换取access_token的票据
         * @return
         */
        /**
         * 
         * 通过code获取access_token
         * 
         * @return
         */
        public static OAuthInfo getAccessToken(String appid, String secret, String code) {
            OAuthInfo oAuthInfo = new OAuthInfo();
            String url = ACCESS_TOKEN_BY_CODE_URL.replace("APPID", appid).replace("SECRET", secret).replace("CODE", code);
            JSONObject jsonObject = doGetStr(url);
            if (jsonObject != null) {
                oAuthInfo.setAccessToken(jsonObject.getString("access_token"));
                oAuthInfo.setOpenId(jsonObject.getString("openid"));
                oAuthInfo.setExpiresIn(jsonObject.getInt("expires_in"));
                oAuthInfo.setRefreshToken(jsonObject.getString("refresh_token"));
                oAuthInfo.setScope(jsonObject.getString("scope"));
            }
            return oAuthInfo;
        }
    
    }
alt  页面展示openidalt 页面展示openid

3.经验总结

上一篇 下一篇

猜你喜欢

热点阅读