python调⽤企业接⼝python调⽤企业接⼝实现关联添加⽤户
# --*-- coding: utf-8 --*--
import json
import urllib2
coreID =
secret =
apisecret =
agent_id =
获取token:
def getToken(coreID, apisecret):
url = 'qyapi.weixin.qq/cgi-bin/gettoken?corpid=%s&corpsecret=%s' % (coreID, apisecret) req = urllib2.Request(url)
result = urllib2.urlopen(req)
access_token = json.ad())
#print access_token['access_token']
return access_token['access_token']
添加⽤户:
def AddWechartUser(userid, name, mobile, email, groupid):
token = getToken(coreID, apisecret)
url = 'qyapi.weixin.qq/cgi-bin/user/create?access_token=%s' % token
values = {
"userid": userid,
"name": name,
"mobile": mobile,
"email": email,
"department": groupid,
}
add_user = json.dumps(values)
request = urllib2.Request(url, add_user)
python新手代码useridresponse = json.loads(urllib2.urlopen(request).read())
#print response
if response['errcode'] == 0:
return "添加成功"
return response['errmsg']
发送告警:
def SendMsg(msg, touser, toparty):
#这⾥以text为例,touser和toparty不可同时为空
token = getToken(coreID, secret)
msgtype = "text"
url = 'qyapi.weixin.qq/cgi-bin/message/send?access_token=%s' % token values = {
"touser": touser,
"toparty": toparty,
"msgtype": msgtype,
"agentid": agent_id,
"text": {
"content": msg
}
}
send = json.dumps(values)
request = urllib2.Request(url, send)
response = json.loads(urllib2.urlopen(request).read())
if response['errcode'] != 0:
return response['errmsg']
return "发送成功"
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论