博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
03: 通讯录管理
阅读量:5329 次
发布时间:2019-06-14

本文共 11397 字,大约阅读时间需要 37 分钟。

目录:企业微信API其他篇

目录:

1.1 概述 & 开启API接口同步

  官方文档    

1.2 成员管理

  1、创建成员

      官方文档

# -*- coding:UTF-8 -*-import urllib2import urllibimport jsondef 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 rettoken_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、读取成员

      官方文档

# -*- coding:UTF-8 -*-import urllib2import urllibimport jsondef 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 rettoken_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 retread_user_info(token_id, 'XiaoNaiQiang')
读取成员XiaoNaiQiang信息

  3、更新成员

      官方文档

# -*- coding:UTF-8 -*-import urllib2import urllibimport jsondef 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 rettoken_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、删除成员

      官方文档:

# -*- coding:UTF-8 -*-import urllib2import urllibimport jsondef 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 rettoken_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 retdelete_user_info(token_id,'zhangsan')
删除成员

  5、批量删除成员 

      官方文档

# -*- coding:UTF-8 -*-import urllib2import urllibimport jsondef 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 rettoken_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、获取部门成员基本信息

      官方文档

# -*- coding:UTF-8 -*-import urllib2import urllibimport jsondef 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 rettoken_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 retget_dept_users(token_id,'3')
获取部门成员基本信息

  7、获取部门成员详细信息 

      官方文档

# -*- coding:UTF-8 -*-import urllib2import urllibimport jsondef 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 rettoken_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 retget_dept_users(token_id,'3')
获取部门成员详细信息

1.3 部门管理

  1、创建部门

      官方文档

# -*- coding:UTF-8 -*-import urllib2import urllibimport jsondef 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 rettoken_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、更新部门

      官方文档

# -*- coding:UTF-8 -*-import urllib2import urllibimport jsondef 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 rettoken_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、删除部门

      官方文档

# -*- coding:UTF-8 -*-import urllib2import urllibimport jsondef 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 rettoken_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 retdelete_dept(token_id,5)
删除部门

  4、获取部门列表

      官方文档

# -*- coding:UTF-8 -*-import urllib2import urllibimport jsondef 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 rettoken_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 retdelete_dept(token_id,1)
获取指定部门及其下的子部门

 

转载于:https://www.cnblogs.com/xiaonq/p/8869466.html

你可能感兴趣的文章
2012年最佳30款免费 WordPress 主题
查看>>
在Silverlight中使用HierarchicalDataTemplate为TreeView实现递归树状结构
查看>>
HDU-1150 Machine Schedule 二分图匹配
查看>>
单例模式的5种写法
查看>>
安卓问题报告小记(四):Some projects cannot be imported because they already exist in the workspace...
查看>>
显示地图
查看>>
无线通信基础(一):无线网络演进
查看>>
如何在工作中快速成长?阿里资深架构师给工程师的10个简单技巧
查看>>
2017.6.4 入门组 NO.4——猜数
查看>>
Eclipse 下载安装
查看>>
WebSocket 时时双向数据,前后端(聊天室)
查看>>
关于cocoa 运行时runtime
查看>>
关于python中带下划线的变量和函数 的意义
查看>>
asp.net 写入excel时,不能更新。数据库或对象为只读。
查看>>
题1简化版
查看>>
linux清空日志文件内容 (转)
查看>>
jsp中对jstl一些标签的引用方式
查看>>
100. Same Tree
查看>>
mkdir命令(转)
查看>>
安卓第十三天笔记-服务(Service)
查看>>