python-对接钉钉,获取access_token

1.首先注册应用,获取 appkey、appsecret

api_url = "https://oapi.dingtalk.com/gettoken?appkey=%s&appsecret=%s"%(appkey,appsecret)

def get_token():
    # try:
    res = requests.get(api_url)
    if res.status_code == 200:
        str_res = res.text
        token = (json.loads(str_res)).get('access_token')
        return token

2.此时拿到的token就是access_token,可以用它来发送请求了

def departList():
    # 所有部门信息
    url = 'https://oapi.dingtalk.com/department/list?access_token={}'.format(get_token())
    ret = json.loads(requests.get(url).text)
    department = ret.get('department')
    departList = []
    for department_info in department:
        departdict = {}
        departdict['name'] = department_info.get('name')
        departdict['id'] = department_info.get('id')
        departdict['parentid'] = department_info.get('parentid')
        departList.append(departdict)
    return departList

你可能感兴趣的:(python-对接钉钉,获取access_token)