微信公众平台修改客服账号接口java实现

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

修改客服帐号

开发者可以通过本接口为公众号修改客服账号。该接口调用请求如下:

http请求方式: POST


https://api.weixin.qq.com/customservice/kfaccount/update?access_token=ACCESS_TOKEN

POST数据示例如下:

{
     "kf_account" : "test1@test",
     "nickname" : "客服1",
     "password" : "pswmd5",
}

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

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

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


java实现代码

我们在微信公众平台添加多客服账号接口 这篇文章中添加了客服账号,再此基础上我们来实现修改客服账号接口。

package com.wepayweb.weixin.test;
/***
 * @author V型知识库 www.vxzsk.com
 
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
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;
     }
      
   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);
        
           
        
    }
}

复制上述代码 配置好参数值,即可实现



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

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