[Exercise] Obtain DingTalk internal application token & sub-department ID list through python SDK

Code first

import dingtalk.api
import requests
# 拼接URL,以便用于GET请求
from urllib.parse import urlencode

getToken_url = "https://oapi.dingtalk.com/gettoken"
params = {"appkey": "your_appkey", "appsecret": "your_appsecret"}
query_string = urlencode(params)
full_url = f"{getToken_url}?{query_string}"
# print(full_url)
#获取token请求
req = dingtalk.api.OapiGettokenRequest(full_url)
access_token = ''
try:
    resp = req.getResponse()
    access_token = resp['access_token']
    # print("应用的access_token为:{}".format(access_token))
except Exception as e:
    print(e)

#获取【部门管理V2.0-获取子部门ID列表】POST请求
getDep_IDurl = "https://oapi.dingtalk.com/topapi/v2/department/listsubid"
params = {"access_token": access_token}
headers = {"Content-Type": "application/json"}
data = {"dept_id": "your_dept_id"}

response = requests.post(getDep_IDurl, params=params, headers=headers, json=data)
# print(type(response))
print(type(response.content))
print(response.content)
print("----- ----- ----- -----")
print(type(response.text))
print(response.text)
print("----- ----- ----- -----")
print(type(response.headers))
print(response.headers)
print("----- ----- ----- -----")

print(type(response.status_code))
print(response.status_code)


Steps:

【1】Download DingTalk python SDK

Download link: https://open.dingtalk.com/document/resourcedownload/download-server-sdk

【2】Introduce the SDK package into the project

【3】Create test cases

The specific code is as above.

【4】Obtain application authorization and related credentials

 【5】Return results

Code explanation:

1. Import the necessary libraries: dingtalk.api and requests. These libraries provide the functionality required to interact with the DingTalk API.

import dingtalk.api
import requests

2. Import the urlencode function, which is used to encode parameters into URL format. This function will be used in later steps.

from urllib.parse import urlencode

3. Splice URLs to obtain accesstoken. getToken_url is the URL to obtain accesstoken, and params is a dictionary containing appkey and appsecret. The urlencode function encodes params into URL format, and then concatenates it with getToken_url to get the complete URL. req is a dingtalk.api.OapiGettokenRequest object, used to send requests to obtain accesstoken. access_token is an empty string and will be assigned in a later step.

getToken_url = "https://oapi.dingtalk.com/gettoken"
params = {"appkey": "your_appkey", "appsecret": "your_appsecret"}
query_string = urlencode(params)
full_url = f"{getToken_url}?{query_string}"
req = dingtalk.api.OapiGettokenRequest(full_url)
access_token = ''

4. Send a request to obtain accesstoken and assign the returned access_token to the access_token variable. If the request fails, an error message will be printed.

try:
    resp = req.getResponse()
    access_token = resp['access_token']
except Exception as e:
    print(e)

5. Get the sub-department ID list. getDep_IDurl is the URL to get the list of subdepartment IDs, and params is a dictionary containing access_token. Headers is a dictionary containing Content-Type, specifying that the content type of the request is JSON. data is a dictionary containing dept_id, specifying the department ID to obtain a list of sub-department IDs. response is the response object returned after sending a request to obtain the subdepartment ID list.

getDep_IDurl = "https://oapi.dingtalk.com/topapi/v2/department/listsubid"
params = {"access_token": access_token}
headers = {"Content-Type": "application/json"}
data = {"dept_id": "your_dept_id"}
response = requests.post(getDep_IDurl, params=params, headers=headers, json=data)

6. Print the type, content, headers and status code of the response content. This information will help us understand the structure and content of the response.
 

print(type(response.content))
print(response.content)
print("----- ----- ----- -----")
print(type(response.text))
print(response.text)
print("----- ----- ----- -----")
print(type(response.headers))
print(response.headers)
print("----- ----- ----- -----")
print(type(response.status_code))
print(response.status_code)

Guess you like

Origin blog.csdn.net/qq_23938507/article/details/130551710