微信公众号第三方平台开发PYTHON教程 PART 4
来源:互联网 发布:java实现md5解密算法 编辑:程序博客网 时间:2024/05/22 20:03
github地址:cppfun@wechat-open-third-party-dev
在开始本节之前,你需要先阅读前三节的内容:
微信公众号第三方平台开发python教程 Part 1
微信公众号第三方平台开发python教程 Part 2
微信公众号第三方平台开发python教程 Part 3
本节将使用 授权码 换取 公众号的接口调用凭据authorizer_access_token和授权信息authorization_info。
微信的文档对小白来讲是种折磨,不过你还是要去看一下,这样再开发其他功能点就比较简单了。
我们直接上代码。我写得代码应该比较容易理解。
def get_authorization_info(self, authorization_code):
url = 'https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=%s' \
% self.get_com_access_token()
payload = {
"component_appid": self.component_appid,
"authorization_code": authorization_code
}
headers = {'content-type': 'application/json'}
response = requests.post(url, data=json.dumps(payload), headers=headers)
data = response.json()
return data['authorization_info']
url = 'https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=%s' \
% self.get_com_access_token()
payload = {
"component_appid": self.component_appid,
"authorization_code": authorization_code
}
headers = {'content-type': 'application/json'}
response = requests.post(url, data=json.dumps(payload), headers=headers)
data = response.json()
return data['authorization_info']
这里多了个参数,authorization_code,这个东东从哪来?
我们看下我是怎么调用该方法的:
elif infoType == 'authorized':
# load file
json_file = open('com_ticket.json')
data = json.load(json_file)
json_file.close()
if data['ComponentVerifyTicket'] == '':
return
ComponentVerifyTicket = data['ComponentVerifyTicket']
wxOpenSDK = WxOpenSDK(ticket=ComponentVerifyTicket)
authorizerAppid = ticket_xml.find('AuthorizerAppid').text
authorizationCode = ticket_xml.find('AuthorizationCode').text
codeExpiredTime = ticket_xml.find('AuthorizationCodeExpiredTime').text
now = time.time()
now = int(now)+7000
info = wxOpenSDK.get_authorization_info(authorization_code=authorizationCode)
authorization_info = Authorization_info(is_authorized=True,
authorizer_appid=authorizerAppid,
authorizer_access_token=info['authorizer_access_token'],
token_expires_time=now,
authorizer_refresh_token=info['authorizer_refresh_token'],
authorization_code=authorizationCode,
code_expires_time=codeExpiredTime)
authorization_info.save()
# load file
json_file = open('com_ticket.json')
data = json.load(json_file)
json_file.close()
if data['ComponentVerifyTicket'] == '':
return
ComponentVerifyTicket = data['ComponentVerifyTicket']
wxOpenSDK = WxOpenSDK(ticket=ComponentVerifyTicket)
authorizerAppid = ticket_xml.find('AuthorizerAppid').text
authorizationCode = ticket_xml.find('AuthorizationCode').text
codeExpiredTime = ticket_xml.find('AuthorizationCodeExpiredTime').text
now = time.time()
now = int(now)+7000
info = wxOpenSDK.get_authorization_info(authorization_code=authorizationCode)
authorization_info = Authorization_info(is_authorized=True,
authorizer_appid=authorizerAppid,
authorizer_access_token=info['authorizer_access_token'],
token_expires_time=now,
authorizer_refresh_token=info['authorizer_refresh_token'],
authorization_code=authorizationCode,
code_expires_time=codeExpiredTime)
authorization_info.save()
当公众号授权成功后,我们会接收到来自微信服务器推送的xml,我们对其进行解析就可以拿到authorizationCode,有点抽象对吧?您先不用理解深层次的东西,先把这个方法放到class WxOpenSDK中,我们的class WxOpenSDK代码现在如下:
class WxOpenSDK:
def __init__(self, ticket):
self.component_appid = component_appid
self.component_appsecret = component_appsecret
self.ticket = ticket
# something below...
def get_com_access_token(self):
# load file
json_file = open('com_access_token.json')
data = json.load(json_file)
json_file.close()
component_access_token = data['component_access_token']
now = time.time()
if data['expire_time'] < now:
url = "https://api.weixin.qq.com/cgi-bin/component/api_component_token"
payload = {'component_appid': self.component_appid,
'component_appsecret': self.component_appsecret,
'component_verify_ticket': self.ticket}
headers = {'content-type': 'application/json'}
response = requests.post(url, data=json.dumps(payload), headers=headers)
component_access_token = json.loads(response.text)['component_access_token']
data['component_access_token'] = component_access_token
data['expire_time'] = int(now) + 7000
# save file
json_file = open('com_access_token.json', 'w')
json_file.write(json.dumps(data))
json_file.close()
return component_access_token
def get_pre_auth_code(self):
# load file
json_file = open('pre_auth_code.json')
data = json.load(json_file)
json_file.close()
pre_auth_code = data['pre_auth_code']
now = time.time()
if data['expire_time'] < now:
url = "https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=%s"\
% self.get_com_access_token()
payload = {'component_appid': self.component_appid}
headers = {'content-type': 'application/json'}
response = requests.post(url, data=json.dumps(payload), headers=headers)
pre_auth_code = json.loads(response.text)['pre_auth_code']
data['pre_auth_code'] = pre_auth_code
data['expire_time'] = int(now) + 1100
# save file
json_file = open('pre_auth_code.json', 'w')
json_file.write(json.dumps(data))
json_file.close()
return pre_auth_code
def get_authorization_info(self, authorization_code):
url = 'https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=%s' \
% self.get_com_access_token()
payload = {
"component_appid": self.component_appid,
"authorization_code": authorization_code
}
headers = {'content-type': 'application/json'}
response = requests.post(url, data=json.dumps(payload), headers=headers)
data = response.json()
return data['authorization_info']
def __init__(self, ticket):
self.component_appid = component_appid
self.component_appsecret = component_appsecret
self.ticket = ticket
# something below...
def get_com_access_token(self):
# load file
json_file = open('com_access_token.json')
data = json.load(json_file)
json_file.close()
component_access_token = data['component_access_token']
now = time.time()
if data['expire_time'] < now:
url = "https://api.weixin.qq.com/cgi-bin/component/api_component_token"
payload = {'component_appid': self.component_appid,
'component_appsecret': self.component_appsecret,
'component_verify_ticket': self.ticket}
headers = {'content-type': 'application/json'}
response = requests.post(url, data=json.dumps(payload), headers=headers)
component_access_token = json.loads(response.text)['component_access_token']
data['component_access_token'] = component_access_token
data['expire_time'] = int(now) + 7000
# save file
json_file = open('com_access_token.json', 'w')
json_file.write(json.dumps(data))
json_file.close()
return component_access_token
def get_pre_auth_code(self):
# load file
json_file = open('pre_auth_code.json')
data = json.load(json_file)
json_file.close()
pre_auth_code = data['pre_auth_code']
now = time.time()
if data['expire_time'] < now:
url = "https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=%s"\
% self.get_com_access_token()
payload = {'component_appid': self.component_appid}
headers = {'content-type': 'application/json'}
response = requests.post(url, data=json.dumps(payload), headers=headers)
pre_auth_code = json.loads(response.text)['pre_auth_code']
data['pre_auth_code'] = pre_auth_code
data['expire_time'] = int(now) + 1100
# save file
json_file = open('pre_auth_code.json', 'w')
json_file.write(json.dumps(data))
json_file.close()
return pre_auth_code
def get_authorization_info(self, authorization_code):
url = 'https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=%s' \
% self.get_com_access_token()
payload = {
"component_appid": self.component_appid,
"authorization_code": authorization_code
}
headers = {'content-type': 'application/json'}
response = requests.post(url, data=json.dumps(payload), headers=headers)
data = response.json()
return data['authorization_info']
暂时的迷茫时可以接受的,这就像读一本书,可能第一次你是有很多疑惑的,等到你阅读到后面再回过头来看,很多东西就会明白。
引用一句话,站的高度不同,视野也就不同,最后导致你的想法也会不同。
然后我们进行第五节的讲解。
0 0
- 微信公众号第三方平台开发PYTHON教程 PART 4
- 微信公众号第三方平台开发PYTHON教程 PART 1
- 微信公众号第三方平台开发PYTHON教程 PART 2
- 微信公众号第三方平台开发PYTHON教程 PART 3
- 微信公众号第三方平台开发PYTHON教程 PART 5
- 微信公众号第三方平台开发PYTHON教程 PART 6
- 微信公众号第三方平台开发PYTHON教程 PART 7
- 微信公众号第三方平台开发PYTHON教程 PART 8
- 微信公众号第三方平台开发PYTHON教程 PART 9
- 微信开放平台 公众号第三方平台开发 教程二 创建公众号第三方平台
- 微信开放平台公众号第三方平台开发 教程一 平台介绍
- 微信开放平台公众号第三方平台开发 教程一 平台介绍
- 微信开放平台公众号第三方平台开发 教程一 平台介绍
- 微信开放平台 公众号第三方平台开发 教程三 一键登录授权给第三方平台
- 微信公众号第三方开发之一创建微信公众号第三方平台
- 微信开放平台 公众号第三方平台开发 教程五 代公众号发起网页授权源码
- 微信公众号第三方平台开发 授权流程
- 微信公众号第三方平台开发笔记--01创建第三方平台
- 【POJ 2398】Toy Storage(计算几何)
- 声学特征(四) pitch-yin代码实现
- Core Data入门
- 【EF系列】EntityFrameWork实体关系映射
- [SDOI2016]sequence 解题报告
- 微信公众号第三方平台开发PYTHON教程 PART 4
- MyApplication
- scaleType
- java的动态绑定与静态绑定
- 动态规划之切割钢条
- 安装mysqldb遇到c1083问题的解决办法
- Linux initrd详解
- 微信公众号第三方平台开发PYTHON教程 PART 5
- 继承中的向上类型转换