【拉勾三期作业贴】企业微信接口实战(一)

https://github.com/gqxjones/MyprojectHogwarts/blob/master/httpTest/test_department.py

import requests

corpid = ‘ww01ad7285c3b2f87c’
corpsecret = ‘hzizt4fesrs-Eg2uz8aYC4a9yZHphA32Lqb3R57Nzuw’

def get_token():
url = f’https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={corpsecret}
result = requests.get(url).json()
return result[‘access_token’]

def test_get():
url = f’https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token={get_token()}&id=Zurich
print(requests.get(url).json())

def test_AddDp():
url = f’https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token={get_token()}
data = {
“name”: “测试2”,
“name_en”: “test2”,
“parentid”: 3,
“order”: 3,
}
print(requests.post(url, json=data).json())

def test_UpdateDp():
url =f’https://qyapi.weixin.qq.com/cgi-bin/department/update?access_token={get_token()}
data= {
“id”: 3,
“name”: “测试测试”
}
print(requests.post(url, json=data).json())

def test_Delete():
url = f’https://qyapi.weixin.qq.com/cgi-bin/department/delete?access_token={get_token()}&id=2
print(requests.get(url).json())

https://github.com/zurichzy/hogw3-cuo

import requests
# 获取token
corpid="ww342da4248a10a8b7"
corpsecret="hpy6BnouorR5z0kckOWVAlY1f80xiJfs9iDnrVu-A3s"
def get_token():
    url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={corpsecret}"
    result =requests.get(url).json()
    return result["access_token"]
# 获取成员列表
def test_get():
    url = f"https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token={get_token()}&id=4"
    print(requests.get(url).json())
# 添加部门
def test_add():
    url = f"https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token={get_token()}"
    data = {
        "name": "66",
        "parentid": 1,
    }
    print(requests.post(url, json=data).json())
# 更新部门列表
def test_update():
    url = f"https://qyapi.weixin.qq.com/cgi-bin/department/update?access_token={get_token()}"
    data = {
        "id": 4,
        "name": "测试花店",
    }
    print(requests.post(url, json=data).json())
# 删除部门
def test_delete():
    url = f"https://qyapi.weixin.qq.com/cgi-bin/department/delete?access_token={get_token()}&id=5"
    print(requests.delete(url).json())




import resquests

corpid = "ww1016c2172d51867a"
corpsecret = "FBwsqfqrwCLOBRgluDnB2aGHeexEG0C54GJRzJEYA7Y"

def get_token():
	token_url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={corpsecret}"
	res = resquests.get(token_url)
	return res["access_token"]

#获取部门信息
def get_department(ID=None):
	get_department_url = f'https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token={get_token()}&id={ID}'
	res = resquests.get(get_department_url)
	print(res)

#根据ID删除部门
def del_department(ID):
	del_url = f'https://qyapi.weixin.qq.com/cgi-bin/department/delete?access_token={get_token()}&id={ID}'
	res = resquests.get(del_url)

def update_department(data):
	del_url = f'https://qyapi.weixin.qq.com/cgi-bin/department/update?access_token={get_token()}'
        data = {
           "id": 2,
           "name": "广州研发中心",
           "name_en": "RDGZ",
           "parentid": 1,
           "order": 1
         }	
        res = resquests.post(del_url,json=data)

def create_department(data):
	create_url = f'https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token={get_token()}'
	data={
          "name": "广州研发中心",
          "name_en": "RDGZ",
           "parentid": 1,
           "order": 1,
           "id": 2
        }
        res = resquests.post(create_url,json=data)

import requests

def get_access_token():
corpid = “wwb34ad3547d53e075”
corpsecret = “FdXX3sDyqUg7idK4BbLq8FDLeNiPySqtup9JemCfJq4”
# 前面加上f 代表变量引用,变量用{}表示就可以了
url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={corpsecret}"

re = requests.get(url)
print(re.json())
return re.json()["access_token"]

def test_get_dep():
url = f"https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token={get_access_token()}"
print(requests.get(url).json())
return requests.get(url).json()

def test_add_dep():
url = f"https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token={get_access_token()}"
data = {
“name”: “测试部门1”,
“name_en”: “CSBM1”,
“parentid”: 1
}
re = requests.post(url=url,json=data)
print(re.json())

def test_update_dep():
url = f"https://qyapi.weixin.qq.com/cgi-bin/department/update?access_token={get_access_token()}"
data = {
“id”: 2,
“name”: “白菜”,
“name_en”: “BC”
}
print(requests.post(url=url, json=data))

def test_del_dep():
print(test_get_dep()[“department”][1][‘id’])
url = f"https://qyapi.weixin.qq.com/cgi-bin/department/delete?access_token={get_access_token()}&id={test_get_dep()[‘department’][1][‘id’]}"
print(requests.get(url))