03: 通讯录管理
目录:企业微信API其他篇
目录:
1.1 概述 & 开启API接口同步 返回顶部
官方文档:http://work.weixin.qq.com/api/doc#10016
1.2 成员管理 返回顶部
1、创建成员
官方文档:http://work.weixin.qq.com/api/doc#10018
# -*- coding:UTF-8 -*- import urllib2 import urllib import json def get_access_token(): '''获取认证access_token值''' url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?' # corpid: 每个企业都拥有唯一的corpid corpsecret: 通讯录管理secret para = {'corpid':'ww2f9a1a85f1806981','corpsecret':'bvAUJ2OYnjB4eAlCpSdH2_0CSyTi6403ZAgtIVVpFpA'} req = urllib2.Request(url + urllib.urlencode(para)) ret = urllib2.urlopen(req) ret = json.loads(ret.read()) return ret token_id = get_access_token().get('access_token') data = { "userid": "zhangsan", "name": "张三", "english_name": "jackzhang", "mobile": "15913215421", "department": [1, 3], } def create_user(token_id,data): '''创建用户 :param token_id: 用于认证的access_token :param data: 提交创建用户的信息 ''' headers = {'Content-Type': 'application/json'} url = "https://qyapi.weixin.qq.com/cgi-bin/user/create?access_token=%s"%(token_id) request = urllib2.Request(url=url, headers=headers, data=json.dumps(data)) response = urllib2.urlopen(request) print response.read() # 返回结果:{"errcode":0,"errmsg":"created"} create_user(token_id,data)
2、读取成员
官方文档:http://work.weixin.qq.com/api/doc#10019
# -*- coding:UTF-8 -*- import urllib2 import urllib import json def get_access_token(): url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?' # corpid: 每个企业都拥有唯一的corpid corpsecret: 通讯录管理secret para = {'corpid':'ww2f9a1a85f1806981','corpsecret':'bvAUJ2OYnjB4eAlCpSdH2_0CSyTi6403ZAgtIVVpFpA'} req = urllib2.Request(url + urllib.urlencode(para)) ret = urllib2.urlopen(req) ret = json.loads(ret.read()) return ret token_id = get_access_token().get('access_token') def read_user_info(token_id,userid): ''' 获取执行用户信息 :param token_id: 用于认证的access_token :param userid: 用户账号id ''' url = 'https://qyapi.weixin.qq.com/cgi-bin/user/get?' para = {'access_token': token_id, 'userid': userid} req = urllib2.Request(url + urllib.urlencode(para)) ret = urllib2.urlopen(req) ret = json.loads(ret.read()) print ret read_user_info(token_id, 'XiaoNaiQiang')
3、更新成员
官方文档:http://work.weixin.qq.com/api/doc#10020
# -*- coding:UTF-8 -*- import urllib2 import urllib import json def get_access_token(): url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?' # corpid: 每个企业都拥有唯一的corpid corpsecret: 通讯录管理secret para = {'corpid':'ww2f9a1a85f1806981','corpsecret':'bvAUJ2OYnjB4eAlCpSdH2_0CSyTi6403ZAgtIVVpFpA'} req = urllib2.Request(url + urllib.urlencode(para)) ret = urllib2.urlopen(req) ret = json.loads(ret.read()) return ret token_id = get_access_token().get('access_token') data = { "userid": "zhangsan", # 只有userid是必须提供的,其他信息都不是必须的 "name": "张三", "mobile": "18588888889", } def update_user_info(token_id,data): '''更新用户信息 :param token_id: 用于认证的access_token :param data: 需要修改成的内容 ''' headers = {'Content-Type': 'application/json'} url = "https://qyapi.weixin.qq.com/cgi-bin/user/update?access_token=%s" % (token_id) request = urllib2.Request(url=url, headers=headers, data=json.dumps(data)) response = urllib2.urlopen(request) print response.read() # 返回结果:{"errcode":0,"errmsg":"updated"} update_user_info(token_id,data)
4、删除成员
官方文档:http://work.weixin.qq.com/api/doc#10030
# -*- coding:UTF-8 -*- import urllib2 import urllib import json def get_access_token(): url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?' # corpid: 每个企业都拥有唯一的corpid corpsecret: 通讯录管理secret para = {'corpid':'ww2f9a1a85f1806981','corpsecret':'bvAUJ2OYnjB4eAlCpSdH2_0CSyTi6403ZAgtIVVpFpA'} req = urllib2.Request(url + urllib.urlencode(para)) ret = urllib2.urlopen(req) ret = json.loads(ret.read()) return ret token_id = get_access_token().get('access_token') def delete_user_info(token_id, userid): ''' 删除执行成员 :param token_id: 用于认证的access_token :param userid: 用户账号id ''' url = 'https://qyapi.weixin.qq.com/cgi-bin/user/delete?' para = {'access_token': token_id, 'userid': userid} req = urllib2.Request(url + urllib.urlencode(para)) ret = urllib2.urlopen(req) ret = json.loads(ret.read()) print ret delete_user_info(token_id,'zhangsan')
5、批量删除成员
官方文档:http://work.weixin.qq.com/api/doc#10060
# -*- coding:UTF-8 -*- import urllib2 import urllib import json def get_access_token(): '''获取认证access_token值''' url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?' # corpid: 每个企业都拥有唯一的corpid corpsecret: 通讯录管理secret para = {'corpid':'ww2f9a1a85f1806981','corpsecret':'bvAUJ2OYnjB4eAlCpSdH2_0CSyTi6403ZAgtIVVpFpA'} req = urllib2.Request(url + urllib.urlencode(para)) ret = urllib2.urlopen(req) ret = json.loads(ret.read()) return ret token_id = get_access_token().get('access_token') data = { "useridlist": ["zhangsan", "lisi"] } def batch_delete_users(token_id,data): '''创建用户 :param token_id: 用于认证的access_token :param data: 提交需要删除用户的userid ''' headers = {'Content-Type': 'application/json'} url = "https://qyapi.weixin.qq.com/cgi-bin/user/batchdelete?access_token=%s"%(token_id) request = urllib2.Request(url=url, headers=headers, data=json.dumps(data)) response = urllib2.urlopen(request) print response.read() # 返回结果:{"errcode":0,"errmsg":"deleted"} batch_delete_users(token_id,data)
6、获取部门成员基本信息
官方文档:http://work.weixin.qq.com/api/doc#10061
# -*- coding:UTF-8 -*- import urllib2 import urllib import json def get_access_token(): url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?' # corpid: 每个企业都拥有唯一的corpid corpsecret: 通讯录管理secret para = {'corpid':'ww2f9a1a85f1806981','corpsecret':'bvAUJ2OYnjB4eAlCpSdH2_0CSyTi6403ZAgtIVVpFpA'} req = urllib2.Request(url + urllib.urlencode(para)) ret = urllib2.urlopen(req) ret = json.loads(ret.read()) return ret token_id = get_access_token().get('access_token') def get_dept_users(token_id, department_id): ''' 获取指定部门成员信息 :param token_id: 用于认证的access_token :param department_id: 部门id ''' url = 'https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?' para = {'access_token': token_id, 'department_id':department_id} req = urllib2.Request(url + urllib.urlencode(para)) ret = urllib2.urlopen(req) ret = json.loads(ret.read()) print ret get_dept_users(token_id,'3')
7、获取部门成员详细信息
官方文档:http://work.weixin.qq.com/api/doc#10063
# -*- coding:UTF-8 -*- import urllib2 import urllib import json def get_access_token(): url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?' # corpid: 每个企业都拥有唯一的corpid corpsecret: 通讯录管理secret para = {'corpid':'ww2f9a1a85f1806981','corpsecret':'bvAUJ2OYnjB4eAlCpSdH2_0CSyTi6403ZAgtIVVpFpA'} req = urllib2.Request(url + urllib.urlencode(para)) ret = urllib2.urlopen(req) ret = json.loads(ret.read()) return ret token_id = get_access_token().get('access_token') def get_dept_users(token_id, department_id): ''' 获取指定部门成员详细信息 :param token_id: 用于认证的access_token :param department_id: 部门id ''' url = 'https://qyapi.weixin.qq.com/cgi-bin/user/list?' para = {'access_token': token_id, 'department_id': department_id} req = urllib2.Request(url + urllib.urlencode(para)) ret = urllib2.urlopen(req) ret = json.loads(ret.read()) print ret get_dept_users(token_id,'3')
1.3 部门管理 返回顶部
1、创建部门
官方文档:http://work.weixin.qq.com/api/doc#10076
# -*- coding:UTF-8 -*- import urllib2 import urllib import json def get_access_token(): '''获取认证access_token值''' url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?' # corpid: 每个企业都拥有唯一的corpid corpsecret: 通讯录管理secret para = {'corpid':'ww2f9a1a85f1806981','corpsecret':'bvAUJ2OYnjB4eAlCpSdH2_0CSyTi6403ZAgtIVVpFpA'} req = urllib2.Request(url + urllib.urlencode(para)) ret = urllib2.urlopen(req) ret = json.loads(ret.read()) return ret token_id = get_access_token().get('access_token') data = { "name": "广州研发中心", # 部门名称(必填) "parentid": 1, # 父部门id(必填) "id": 5 # 部门id,不填将自动生成id(选填) } def create_dept(token_id,data): '''创建部门 :param token_id: 用于认证的access_token :param data: 创建部门信息 ''' headers = {'Content-Type': 'application/json'} url = "https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token=%s"%(token_id) request = urllib2.Request(url=url, headers=headers, data=json.dumps(data)) response = urllib2.urlopen(request) print response.read() # 返回结果:{"errcode":0,"errmsg":"created","id":5} create_dept(token_id,data)
2、更新部门
官方文档:http://work.weixin.qq.com/api/doc#10077
# -*- coding:UTF-8 -*- import urllib2 import urllib import json def get_access_token(): '''获取认证access_token值''' url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?' # corpid: 每个企业都拥有唯一的corpid corpsecret: 通讯录管理secret para = {'corpid':'ww2f9a1a85f1806981','corpsecret':'bvAUJ2OYnjB4eAlCpSdH2_0CSyTi6403ZAgtIVVpFpA'} req = urllib2.Request(url + urllib.urlencode(para)) ret = urllib2.urlopen(req) ret = json.loads(ret.read()) return ret token_id = get_access_token().get('access_token') data = { "name": "广州研发中心部门", # 部门名称(必填) "parentid": 1, # 父部门id(必填) "id": 5 # 部门id,不填将自动生成id(选填) } def update_dept(token_id,data): '''修改部门 :param token_id: 用于认证的access_token :param data: 需要修改的部门信息 ''' headers = {'Content-Type': 'application/json'} url = "https://qyapi.weixin.qq.com/cgi-bin/department/update?access_token=%s"%(token_id) request = urllib2.Request(url=url, headers=headers, data=json.dumps(data)) response = urllib2.urlopen(request) print response.read() # 返回结果:{"errcode":0,"errmsg":"updated"} update_dept(token_id,data)
3、删除部门
官方文档:http://work.weixin.qq.com/api/doc#10079
# -*- coding:UTF-8 -*- import urllib2 import urllib import json def get_access_token(): url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?' # corpid: 每个企业都拥有唯一的corpid corpsecret: 通讯录管理secret para = {'corpid':'ww2f9a1a85f1806981','corpsecret':'bvAUJ2OYnjB4eAlCpSdH2_0CSyTi6403ZAgtIVVpFpA'} req = urllib2.Request(url + urllib.urlencode(para)) ret = urllib2.urlopen(req) ret = json.loads(ret.read()) return ret token_id = get_access_token().get('access_token') def delete_dept(token_id,id): '''删除指定部门 :param token_id: 用于认证的access_token :param id: 要删除的部门id ''' url = 'https://qyapi.weixin.qq.com/cgi-bin/department/delete?' para = {'access_token': token_id, 'id': id} req = urllib2.Request(url + urllib.urlencode(para)) ret = urllib2.urlopen(req) ret = json.loads(ret.read()) print ret delete_dept(token_id,5)
4、获取部门列表
官方文档:http://work.weixin.qq.com/api/doc#10093
# -*- coding:UTF-8 -*- import urllib2 import urllib import json def get_access_token(): url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?' # corpid: 每个企业都拥有唯一的corpid corpsecret: 通讯录管理secret para = {'corpid':'ww2f9a1a85f1806981','corpsecret':'bvAUJ2OYnjB4eAlCpSdH2_0CSyTi6403ZAgtIVVpFpA'} req = urllib2.Request(url + urllib.urlencode(para)) ret = urllib2.urlopen(req) ret = json.loads(ret.read()) return ret token_id = get_access_token().get('access_token') def delete_dept(token_id,id): '''删除指定部门 :param token_id: 用于认证的access_token :param id: 要获取的部门id ''' url = 'https://qyapi.weixin.qq.com/cgi-bin/department/list?' para = {'access_token': token_id, 'id': id} req = urllib2.Request(url + urllib.urlencode(para)) ret = urllib2.urlopen(req) ret = json.loads(ret.read()) print ret delete_dept(token_id,1)
作者:学无止境
出处:https://www.cnblogs.com/xiaonq
生活不只是眼前的苟且,还有诗和远方。