from imp import reload
import requests
import json
app_key='XX'
app_secret='XX'
agent_id='XX'
corp_id='XX'
def getAccess():
url = "https://oapi.dingtalk.com/gettoken?appkey={0}&appsecret={1}".format(app_key,app_secret)
jo = json.loads(requests.get(url).text)
return jo['access_token']
def getDp(token):
url = "https://oapi.dingtalk.com/department/list?access_token="+token
print(url)
dp = json.loads(requests.get(url).text)
print(dp)
return dp['department']
def getUsers(token,deptId):
url = "https://oapi.dingtalk.com/user/getDeptMember?access_token={0}&deptId={1}".format(token,deptId)
users = json.loads(requests.get(url).text)
return users['userIds']
def getUserInfo(token,userId):
url = "https://oapi.dingtalk.com/user/get?access_token={0}&userid={1}".format(token,userId)
info = json.loads(requests.get(url).text)
return info['name'],info['userid']
# token
token = '661d389941293ed68edaed413731a1c8'
token = ''
if token=='' :
token = getAccess()
dp = []
if len(dp)==0:
dp = getDp(token)
import cx_Oracle
def save(id,name):
sql = 'update t_user set dingtalk = \'{0}\' where name =\'{1}\' '.format(
id,name)
print('sql:'+sql)XXX
db = cx_Oracle.connect('XX/[email protected]:1521/XXX') # 连接数据库
# print(db.version) # 打印版本看看 显示 11.2.0.1.0
try:
cur = db.cursor() # 游标操作
cur.execute(sql)
db.commit()
print("db success")
except Exception as e:
print("DB Exception:=>")
print(e)
finally:
db.close
for d in dp:
print("开始 部门:"+(d['name'])+'------>')
if d['id']==1:
continue;
users = getUsers(token,d['id'])
for u in users:
username, id = getUserInfo(token, u)
print( 'userId:' + u+' '+username + ':' + id )
save(id,username)