微信公众平台获取所有客服账号接口java实现

2018年06月12日 08:37 | 3157次浏览 作者原创 版权保护

获取所有客服账号

开发者通过本接口,获取公众号中所设置的客服基本信息,包括客服工号、客服昵称、客服登录账号。

http请求方式: GET

https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token=ACCESS_TOKEN

返回说明(正确时的JSON返回结果):

{
    "kf_list": [
        {
            "kf_account": "test1@test", 
            "kf_nick": "ntest1", 
            "kf_id": "1001"
            "kf_headimgurl": " http://mmbiz.qpic.cn/mmbiz/4whpV1VZl2iccsvYbHvnphkyGtnvjfUS8Ym0GSaLic0FD3vN0V8PILcibEGb2fPfEOmw/0"
        }, 
        {
            "kf_account": "test2@test", 
            "kf_nick": "ntest2", 
            "kf_id": "1002"
            "kf_headimgurl": " http://mmbiz.qpic.cn/mmbiz/4whpV1VZl2iccsvYbHvnphkyGtnvjfUS8Ym0GSaLic0FD3vN0V8PILcibEGb2fPfEOmw /0"
        }, 
        {
            "kf_account": "test3@test", 
            "kf_nick": "ntest3", 
            "kf_id": "1003"
            "kf_headimgurl": " http://mmbiz.qpic.cn/mmbiz/4whpV1VZl2iccsvYbHvnphkyGtnvjfUS8Ym0GSaLic0FD3vN0V8PILcibEGb2fPfEOmw /0"
        }
    ]
}

错误时微信会返回错误码等信息,请根据错误码查询错误信息。错误码请参考https://www.vxzsk.com/94.html

接口的统一参数说明

参数
是否必须说明
access_token
调用接口凭证
kf_account完整客服账号,格式为:账号前缀@公众号微信号
kf_nick客服昵称
kf_id客服工号
nickname客服昵称,最长6个汉字或12个英文字符
password客服账号登录密码,格式为密码明文的32位加密MD5值。该密码仅用于在公众平台官网的多客服功能中使用,若不使用多客服功能,则不必设置密码
media该参数仅在设置客服头像时出现,是form-data中媒体文件标识,有filename、filelength、content-type等信息

java实现代码

package com.wepayweb.weixin.test;
/***
 * @author V型知识库 www.vxzsk.com
 
 */
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
 
import net.sf.json.JSONObject;
 
public class TestAcessToken {
    /***V型知识库 www.vxzsk.com
     * 模拟get请求
     * @param url
     * @param charset
     * @param timeout
     * @return
     */
     public static String sendGet(String url, String charset, int timeout)
      {
        String result = "";
        try
        {
          URL u = new URL(url);
          try
          {
            URLConnection conn = u.openConnection();
            conn.connect();
            conn.setConnectTimeout(timeout);
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), charset));
            String line="";
            while ((line = in.readLine()) != null)
            {
             
              result = result + line;
            }
            in.close();
          } catch (IOException e) {
            return result;
          }
        }
        catch (MalformedURLException e)
        {
          return result;
        }
       
        return result;
      }
      /***
       * V型知识库 www.vxzsk.com
       */
     public static String sendPost(String requrl,String param){
         URL url;
          String sTotalString="";  
        try {
            url = new URL(requrl);
             URLConnection connection = url.openConnection(); 
              
             connection.setRequestProperty("accept", "*/*");
             connection.setRequestProperty("connection", "Keep-Alive");
             connection.setRequestProperty("Content-Type", "text/xml");
            // connection.setRequestProperty("Content-Length", body.getBytes().length+"");
             connection.setRequestProperty("User-Agent",
                     "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)");
              
              
                connection.setDoOutput(true);  
                OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "utf-8");  
                out.write(param); // 向页面传递数据。post的关键所在!  
                out.flush();  
                out.close();  
                // 一旦发送成功,用以下方法就可以得到服务器的回应:  
                String sCurrentLine;  
               
                sCurrentLine = "";  
                sTotalString = "";  
                InputStream l_urlStream;  
                l_urlStream = connection.getInputStream();  
                // 传说中的三层包装阿!  
                BufferedReader l_reader = new BufferedReader(new InputStreamReader(  
                        l_urlStream));  
                while ((sCurrentLine = l_reader.readLine()) != null) {  
                    sTotalString += sCurrentLine + "\r\n";  
           
                }  
                 
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  
            
            System.out.println(sTotalString);  
            return sTotalString;
     }
     
     
     /** 
      *  
     * @Title: uploadMedia  
     * Description:上传头像 
     * @author houzhipeng 
     * 
     * @param type 
     * @param kf_account 
     * @param mediaFileUrl  void 
     * @throws 
      */  
     public static void uploadMedia(String type, String kf_account, String mediaFileUrl) {  
    	 
    	 String appid="你公众号基本设置里的应用id";//应用ID
         String appSecret="你公众号基本设置里的应用密钥";//(应用密钥)
         String url ="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+appSecret+"";
         String backData=TestAcessToken.sendGet(url, "utf-8", 10000);
         System.out.println("返回:"+backData);
         JSONObject jsonObject = JSONObject.fromObject(backData);
         String access_token=jsonObject .getString("access_token");
    	 
    	 
         // 拼装请求地址  
        
         String uploadMediaUrl = "http://api.weixin.qq.com/customservice/kfaccount/uploadheadimg?access_token="  
                 + access_token + "&kf_account=" + kf_account;  
   
         // 定义数据分隔符  
         String boundary = "------------7da2e536604c8";  
         try {  
             URL uploadUrl = new URL(uploadMediaUrl);  
             HttpURLConnection uploadConn = (HttpURLConnection) uploadUrl.openConnection();  
             uploadConn.setDoOutput(true);  
             uploadConn.setDoInput(true);  
             uploadConn.setRequestMethod("POST");  
             // 设置请求头Content-Type  
             uploadConn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);  
             // 获取媒体文件上传的输出流(往微信服务器写数据)  
             OutputStream outputStream = uploadConn.getOutputStream();  
   
             URL mediaUrl = new URL(mediaFileUrl);  
             HttpURLConnection meidaConn = (HttpURLConnection) mediaUrl.openConnection();  
             meidaConn.setDoOutput(true);  
             meidaConn.setRequestMethod("GET");  
   
             // 从请求头中获取内容类型  
             String contentType = meidaConn.getHeaderField("Content-Type");  
             // 请求体开始  
             outputStream.write(("--" + boundary + "\r\n").getBytes());  
             outputStream.write(String.format("Content-Disposition: form-data; name=\"media\"; filename=\"file1\"\r\n")  
                     .getBytes());  
             outputStream.write(String.format("Content-Type: %s\r\n\r\n", contentType).getBytes());  
   
             // 获取媒体文件的输入流(读取文件)  
             BufferedInputStream bis = new BufferedInputStream(meidaConn.getInputStream());  
             byte[] buf = new byte[8096];  
             int size = 0;  
             while ((size = bis.read(buf)) != -1) {  
                 // 将媒体文件写到输出流(往微信服务器写数据)  
                 outputStream.write(buf, 0, size);  
             }  
             // 请求体结束  
             outputStream.write(("\r\n--" + boundary + "--\r\n").getBytes());  
             outputStream.close();  
             bis.close();  
             meidaConn.disconnect();  
   
             // 获取媒体文件上传的输入流(从微信服务器读数据)  
             InputStream inputStream = uploadConn.getInputStream();  
             InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");  
             BufferedReader bufferedReader = new BufferedReader(inputStreamReader);  
             StringBuffer buffer = new StringBuffer();  
             String str = null;  
             while ((str = bufferedReader.readLine()) != null) {  
                 buffer.append(str);  
             }  
             bufferedReader.close();  
             inputStreamReader.close();  
             // 释放资源  
             inputStream.close();  
             inputStream = null;  
             uploadConn.disconnect();  
   
             // 使用JSON-lib解析返回结果  
             JSONObject json = JSONObject.fromObject(buffer.toString());
            // JSONObject jsonObject = JSONObject.fromObject(buffer.toString());  
             // 测试打印结果  
             System.out.println("打印测试结果" + json);  
         } catch (Exception e) {  
             String error = String.format("上传媒体文件失败:%s", e);  
             System.out.println(error);  
         }  
     }  
      
   public static void main(String[] args) {
        String appid="你公众号基本设置里的应用id";//应用ID
        String appSecret="你公众号基本设置里的应用密钥";//(应用密钥)
        String url ="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+appSecret+"";
        String backData=TestAcessToken.sendGet(url, "utf-8", 10000);
        System.out.println("返回:"+backData);
        JSONObject jsonObject = JSONObject.fromObject(backData);
        String access_token=jsonObject .getString("access_token");
       /****
        //添加客服
        String add_url = "https://api.weixin.qq.com/customservice/kfaccount/add?access_token="+access_token;
        String param="{\"kf_account\" : \"test1@test\",\"nickname\" : \"客服1\",\"password\" : \"pswmd5\"}";
        String data = TestAcessToken.sendPost(add_url, param);
        System.out.println(data);
         ***/
        //修改客服账号
        /***String update_url="https://api.weixin.qq.com/customservice/kfaccount/update?access_token="+access_token;
        String param="{\"kf_account\" : \"test1@test\",\"nickname\" : \"修改的客服啊啊\",\"password\" : \"pswmd5\"}";
        String data = TestAcessToken.sendPost(update_url, param);
        System.out.println(data);
        ***/
       /*** //删除客服账号
        String del_url="https://api.weixin.qq.com/customservice/kfaccount/del?access_token="+access_token;
        String param="{\"kf_account\" : \"test1@test\",\"nickname\" : \"客服1\",\"password\" : \"pswmd5\"}";
        String data = TestAcessToken.sendPost(del_url, param);
        System.out.println(data);
        ***/
        //获取所有客服账号
        String get_url="https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token="+access_token;
        //String param="{\"kf_account\" : \"test1@test\",\"nickname\" : \"客服1\",\"password\" : \"pswmd5\"}";
        String data = TestAcessToken.sendGet(get_url, "utf-8", 10000);
        System.out.println(data);
        
        
           
        
    }
}



小说《我是全球混乱的源头》
此文章本站原创,地址 https://www.vxzsk.com/1064.html   转载请注明出处!谢谢!

感觉本站内容不错,读后有收获?小额赞助,鼓励网站分享出更好的教程