1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > 微信公众号第三方平台开发PYTHON教程 PART 5

微信公众号第三方平台开发PYTHON教程 PART 5

时间:2022-05-20 13:02:06

相关推荐

微信公众号第三方平台开发PYTHON教程 PART 5

github地址:cppfun@wechat-open-third-party-dev

在开始本节之前,你需要先阅读前四节的内容:

微信公众号第三方平台开发python教程 Part 1

微信公众号第三方平台开发python教程 Part 2

微信公众号第三方平台开发python教程 Part 3

微信公众号第三方平台开发python教程 Part 4

本节第五讲,我们讲讲如何刷新token(刷新授权公众号的接口调用令牌)。

这个其实就更简单了,代码如下:

defget_refresh_authorizer_access_token(self,authorizer_appid,authorizer_refresh_token):

url='https://api./cgi-bin/component/api_authorizer_token?component_access_token=%s'\

%self.get_com_access_token()

payload={

"component_appid":ponent_appid,

"authorizer_appid": authorizer_appid,

"authorizer_refresh_token": authorizer_refresh_token

}

headers={'content-type':'application/json'}

response=requests.post(url,data=json.dumps(payload),headers=headers)

returnresponse.json()

这里需要两个参数:

# authorizer_appid

# 授权方appid

# authorizer_refresh_token

# 授权方的刷新令牌,刷新令牌主要用于公众号第三方平台获取和刷新已授权用户的access_token,只会在授权时刻提供,请妥善保存。

# 我们一般将其保存在数据库中,因为你授权的公众号不止一个

现在我们看看我们的class WxOpenSDK,其代码如下:

classWxOpenCallback:

def__init__(self):

self.token=token

defcheck_signature(self,pams):

ifnotself.token:

returnHttpResponse('TOKEN is not defined!')

msg_signature=pams.get('msg_signature','')

timestamp=pams.get('timestamp','')

nonce=pams.get('nonce','')

tmparr=[self.token,timestamp,nonce]

tmparr.sort()

string=''.join(tmparr)

string=hashlib.sha1(string).hexdigest()

# print signature

# print string

returnmsg_signature==string

classWxOpenSDK:

def__init__(self,ticket):

ponent_appid=component_appid

ponent_appsecret=component_appsecret

self.ticket=ticket

# something below...

defget_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()

ifdata['expire_time']<now:

url="https://api./cgi-bin/component/api_component_token"

payload={'component_appid':ponent_appid,

'component_appsecret':ponent_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()

returncomponent_access_token

defget_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()

ifdata['expire_time']<now:

url="https://api./cgi-bin/component/api_create_preauthcode?component_access_token=%s"\

%self.get_com_access_token()

payload={'component_appid':ponent_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()

returnpre_auth_code

defget_authorization_info(self,authorization_code):

url='https://api./cgi-bin/component/api_query_auth?component_access_token=%s'\

%self.get_com_access_token()

payload={

"component_appid":ponent_appid,

"authorization_code": authorization_code

}

headers={'content-type':'application/json'}

response=requests.post(url,data=json.dumps(payload),headers=headers)

data=response.json()

returndata['authorization_info']

defget_refresh_authorizer_access_token(self,authorizer_appid,authorizer_refresh_token):

url='https://api./cgi-bin/component/api_authorizer_token?component_access_token=%s'\

%self.get_com_access_token()

payload={

"component_appid":ponent_appid,

"authorizer_appid": authorizer_appid,

"authorizer_refresh_token": authorizer_refresh_token

}

headers={'content-type':'application/json'}

response=requests.post(url,data=json.dumps(payload),headers=headers)

returnresponse.json()

接下来,我们会进行第六节。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。