入门指引之查看accesstoken

1. 测试工具查看 https://mp.weixin.qq.com/debug/

  

 

   

 

 

2. 代码获取:

# -*- coding: utf-8 -*-
# filename: basic.py
import urllib.request
import time
import json
class Basic:
    def __init__(self):
        self.__accessToken = ''
        self.__leftTime = 0
    def __real_get_access_token(self):
        appId = "xxxxx"
        appSecret = "xxxxxx"
        postUrl = ("https://api.weixin.qq.com/cgi-bin/token?grant_type=""client_credential&appid=%s&secret=%s" % (appId, appSecret))
        urlResp = urllib.request.urlopen(postUrl)
        urlResp = json.loads(urlResp.read())
        self.__accessToken = urlResp['access_token']
        self.__leftTime = urlResp['expires_in']
    def get_access_token(self):
        if self.__leftTime < 10:
            self.__real_get_access_token()
            return self.__accessToken
    def run(self):
        while(True):
            if self.__leftTime > 10:
                time.sleep(2)
                self.__leftTime -= 2
            else:
                self.__real_get_access_token()
                print("#-----debug---#")
                print(self.__accessToken)
                print(self.__leftTime)

if __name__ == '__main__':
    b = Basic()
    b.run()

 

posted @ 2019-10-15 15:26  AhMay  阅读(749)  评论(0编辑  收藏  举报