原文链接https://blog.csdn.net/weixin_45912307/article/details/109562530
api文档https://work.weixin.qq.com/api/doc/90000/90135/90664

# —— coding :utf-8 ——
# @time:    2020/11/8 15:34
# @IDE:     apiTest
# @Author:  JsonlLu
# @Email:   xxxxxxx@qq.com
# @File:    test_address_book.py
import json
import logging

import requests
class TestWechart:
	corpid = "wwd9fbd4a0d00cb6cd"
    corpsecret = "0qXvfoRkupjQCLk6NNRJcDdKtsE45JJr_Opc71XGmEg"
    def test_token(self):
        url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken"
        params =
         {
            "corpid":self.corpid,
            "corpsecret":self.corpsecret
        }
        res = requests.get(url,params=params)
        print(res.json())
        # assert res.json()["errcode"] == 0
        return res.json()["access_token"]

    def test_creat_new_user(self): # 新建成员对象
        access_token = 'MHWamhxsmOfl76TUi-VHKmLv7eUtowiaLbWI3_q-15J6_s5Yl7Gcz2_1L3JsnIuvF_VhfgaDG6Oj-wROEv5USF0C7kZfMFZ3oVbpHfMMy9yL94FgTpNBpyRW7ABCyXFqMj3w03d7xUVhDUW-UGE807fzoNaZnXY-1rlPzKw_lwigRrdNBIVqxJXnth65fY4KFMOcrZB8nEtC-1mIGhGuJA'
        url = f"https://qyapi.weixin.qq.com/cgi-bin/user/create?access_token={access_token}"
        data = {
            "userid": "mafeifei",
            "name": "马飞飞",
            "alias":"马飞飞",
            "mobile": "13538380480",
            "department": [10],
            "order": 1,
            "position": "自动化测试工程师",
            "gender": "1",
            "email": "mafeifei11@qq.com",
            "is_leader_in_dept": 0,
            "enable": 1,
            "telephone": "13538380480",
            "address": "guangdong guagnzhou",
            "main_department": 1,
            "to_invite": True,
            "external_position": "SQAE"
        }
        r = requests.post(url,json=data)
        print(r.json())

    def test_select_user(self): # 获取单一用户
        url = f"https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token={self.test_token()}&userid=lisi145"
        res = requests.get(url)
        print(res.json())

    def test_update_user(self): # 更新成员信息
        url = f"https://qyapi.weixin.qq.com/cgi-bin/user/update?access_token={self.test_token()}"
        data = {
            "userid": "admin7777",
            "name": "马飞飞111",
            "mobile": "13510329900",
            "department": [2],
            "order": 1,
            "position": "高级测试开发工程师",
            "gender": "1",
            "email": "admin123@qq.com",
            "is_leader_in_dept": 1,
            "enable": 1,
            "telephone": "13510328881",
            "address": "guangdong guangzhou",
            "main_department": 1,
            "to_invite": True,
            "external_position": "SQAE"
        }
        try:
            res = requests.post(url,json=data)
            print(res.text)
        except Exception as e:
            print(e)

    def test_delete_user(self):  # 单一成员删除
        url = f"https://qyapi.weixin.qq.com/cgi-bin/user/delete?access_token={self.test_token()}&userid=maliu666"
        try:
            res = requests.get(url)
            print(res.json())
        except Exception as e:
            logging.exception(e)
    def test_delete_many_users(self):  # 删除多个成员
        url = f"https://qyapi.weixin.qq.com/cgi-bin/user/batchdelete?access_token={self.test_token()}"
        data = {
            "useridlist": ["meiyanyan", "xiyanyan"]
        }
        try:
            res = requests.post(url,data)
            res.json()
        except Exception as e:
            print(e)

01. token的获取

在这里插入图片描述
在这里插入图片描述在这里插入图片描述

02. 新增成员

在这里插入图片描述

03. 查询成员

在这里插入图片描述

04. 更新成员信息

在这里插入图片描述

05. 删除单一成员对象

在这里插入图片描述

06. 删除多个成员对象

在这里插入图片描述

07. 总代码

在这里插入图片描述
在这里插入图片描述

Logo

更多推荐