图像

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

github地址:cppfun@wechat-open-third-party-dev
在讲之前,我需要先去更新一下我刚才写教程的一个缺陷。
当然在本节之前,你需要先阅读前面几节的内容:
微信公众号第三方平台开发python教程 Part 1
微信公众号第三方平台开发python教程 Part 2
微信公众号第三方平台开发python教程 Part 3
微信公众号第三方平台开发python教程 Part 4
微信公众号第三方平台开发python教程 Part 5

本节我们将获取授权方的公众号帐号基本信息,这个也很简单,我们看看代码:

    def get_authorizer_info(self, authorizer_appid):
        url = 'https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token=%s' \
              % self.get_com_access_token()
        payload = {
            "component_appid": self.component_appid,
            "authorizer_appid": authorizer_appid
        }
        headers = {'content-type': 'application/json'}
        response = requests.post(url, data=json.dumps(payload), headers=headers)
        data = response.json()
        return data['authorizer_info']

这个也有一个参数:authorizer_appid,这个参数明显是存储到数据库里面的。
到现在我们的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 get_refresh_authorizer_access_token(self, authorizer_appid, authorizer_refresh_token):
        url = 'https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=%s' \
              % self.get_com_access_token()
        payload = {
            "component_appid": self.component_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)
        return response.json()

    def get_authorizer_info(self, authorizer_appid):
        url = 'https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token=%s' \
              % self.get_com_access_token()
        payload = {
            "component_appid": self.component_appid,
            "authorizer_appid": authorizer_appid
        }
        headers = {'content-type': 'application/json'}
        response = requests.post(url, data=json.dumps(payload), headers=headers)
        data = response.json()
        return data['authorizer_info']

以上的每个代码片段我们都已经讲解结束,接下来我们将进行本次教程第七节,接收微信服务器推送授权的相关通知

发表评论