微信转化接口(2018-06-14)

微信迁移主体 将关注用户转移到新公众号

/**
 * 得到微信公众号用户OPENID
 * @param 原微信accessToken
 */

public static List getOpenIdList(String accessToken)
{
    String total;           //关注该公众账号的总用户数
    String count;           //拉取的OPENID个数,最大值为10000
    JSONObject data;            //列表数据,OPENID的列表
    String next_openid;     //拉取列表的最后一个用户的OPENID
    List openidList =null;
    //http请求方式: GET(请使用https协议)
    //https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID
    String url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token="+accessToken;
    try {
        URL getUrl=new URL(url);
        HttpURLConnection http=(HttpURLConnection)getUrl.openConnection();
        http.setRequestMethod("GET");
        http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        http.setDoOutput(true);
        http.setDoInput(true);
        http.connect();
        //请求成功200提示码
       if( http.getResponseCode()==200 ) {
           InputStream is = http.getInputStream(); 
           int size = is.available(); 
            byte[] b = new byte[size];
            is.read(b);

            String message = new String(b, "UTF-8");

            JSONObject obj=new JSONObject(message);
            total=(String) obj.get("total");
            count=(String) obj.get("count");
            data = (JSONObject)obj.get("data");
            openidList = (List)data.get("openid");
            next_openid=(String) obj.get("next_openid");
       }


        } catch (MalformedURLException e) {
        e.printStackTrace();
        } catch (IOException e) {
        e.printStackTrace();
        }
        return openidList;
}
/**
 * 旧微信公众号OPENID转化为新微信公众号OPENID----
 * @param openidList原微信的用户openid集合  新微信accessToken 原微信appid
 */
public static LinkedHashMap<String,String>  oldOpenIdConvertToNewOpenId(List openidList,String accessToken,String appid)
{

    LinkedHashMap<String,String> map = new LinkedHashMap<String,String>();
    //http请求方式: post(请使用https协议)token为新帐号的token
    //http://api.weixin.qq.com/cgi-bin/changeopenid?access_token=xxxxx
    if( openidList != null && openidList.size() > 0){
        //计算需要转化次数  因为每次微信转化最多100个/次
        int amount = (int)Math.ceil(openidList.size()/100);
        //从下标为0开始截取
        int count = 0;
        //截取list
        List subList;
        for( int i=0;i<amount; i++ ){

            count +=1;
            subList = openidList.subList((count-1)*100, count*100);

            if( amount == count){
                subList = openidList.subList((count-1)*100, openidList.size()+1);
            }
            String url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token="+accessToken;
            try {
                URL getUrl=new URL(url);
                HttpURLConnection http=(HttpURLConnection)getUrl.openConnection();
                http.setDoOutput(true);
                http.setDoInput(true);
                http.setRequestMethod("POST");
                http.setUseCaches(false); 
                http.setInstanceFollowRedirects(true);
                http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
                http.setRequestProperty("Charset", "UTF-8"); 
                http.connect();
                DataOutputStream out = new DataOutputStream(http.getOutputStream());

                String params="{"
                                + "\"from_appid\":"+ appid+","
                                + "\"openid_list\":"+subList+"}";        
                System.out.println(params);
                out.write(params.getBytes("UTF-8"));
                out.flush();
                out.close(); 
                BufferedReader reader = new BufferedReader(new InputStreamReader(http.getInputStream(),"UTF-8"));
                String line;
                String responseStr="";
                while ((line = reader.readLine()) != null){
                    responseStr+=line;
                }
                System.out.println(responseStr);
                JSONObject obj=new JSONObject(responseStr);
                System.out.println(obj.get("errmsg"));

                JSONArray newOpenIdList = JSON.parseArray( obj.get("result_list")+"");
                //遍历得到取到的新旧OPENID
                if( newOpenIdList!=null && newOpenIdList.size()>0){

                    for(int j=0;j<newOpenIdList.size();j++){

                        com.alibaba.fastjson.JSONObject jsonObject = newOpenIdList.getJSONObject(j);
                        String err_msg = jsonObject.get("err_msg")+"";

                        //取到目前还关注公众号的用户,即会返回"ok"
                        if( err_msg != null && err_msg.equals("ok") ){
                            String ori_openid = jsonObject.get("ori_openid")+"";
                            String new_openid = jsonObject.get("new_openid")+"";
                            System.out.println(ori_openid);
                            System.out.println(new_openid);
                            map.put(ori_openid, new_openid);
                        }

                    }
                }

                reader.close();
                http.disconnect();

            }catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (ProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return map;
}

}

阅读更多
个人分类: 微信开发
上一篇JAVA基础重温--Integer,int的区别
下一篇短信---阿里大鱼
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭