微信公众号设置客服帐号的头像接口实现

2018年06月11日 08:01 | 3871次浏览 作者原创 版权保护

设置客服帐号的头像

开发者可调用本接口来上传图片作为客服人员的头像,头像图片文件必须是jpg格式,推荐使用640*640大小的图片以达到最佳效果。该接口调用请求如下:

http请求方式: POST/FORM

http://api.weixin.qq.com/customservice/kfaccount/uploadheadimg?access_token=ACCESS_TOKEN&kf_account=KFACCOUNT

调用示例:使用curl命令,用FORM表单方式上传一个多媒体文件,curl命令的具体用法请自行了解

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

{
     "errcode" : 0,
     "errmsg" : "ok",
}

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



使用curl命令上传图片

在linux系统上执行:

curl -F media=@test.jpg "https://api.weixin.qq.com/customservice/kfaccount/uploadheadimg?access_token=ACCESS_TOKEN&kf_account=KFACCOUNT"


java代码实现设置客服账号头像

 /** 
      *  v型知识库 www.vxzsk.com
     * @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);  
         }  
     }


TestAcessToken.sendGet方法

 /***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;
      }



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

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