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

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

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

继续第三节,获取预授权码pre_auth_code,这个就比较简单了。我们来看代码实现:

     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

这里pre_auth_code的有效期是20分钟,同样我们也把它封装到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
# others code

这节也很简单吧,enjoy it。
接下来我们进行第四节的讲解。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值