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' ]
暂时的迷茫时可以接受的,这就像读一本书,可能第一次你是有很多疑惑的,等到你阅读到后面再回过头来看,很多东西就会明白。
引用一句话,站的高度不同,视野也就不同,最后导致你的想法也会不同。
然后我们进行第五节的讲解。