Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change get_wechat_access_token to be classmethod #13

Merged
merged 1 commit into from
Nov 3, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions wechat/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import logging
import urllib.request
import xml.etree.ElementTree as ET
from WeChatTicket.settings import WECHAT_TOKEN, WECHAT_APPID, WECHAT_SECRET

from django.http import Http404, HttpResponse
from django.template.loader import get_template
Expand Down Expand Up @@ -112,14 +113,17 @@ def __repr__(self):
class WeChatLib(object):

logger = logging.getLogger('wechatlib')
access_token = ''
access_token_expire = datetime.datetime.fromtimestamp(0)
token = WECHAT_TOKEN
appid = WECHAT_APPID
secret = WECHAT_SECRET

def __init__(self, token, appid, secret):
super(WeChatLib, self).__init__()
self.token = token
self.appid = appid
self.secret = secret
self.access_token = ''
self.access_token_expire = datetime.datetime.fromtimestamp(0)

def check_signature(self, signature, timestamp, nonce):
tmp_list = sorted([self.token, timestamp, nonce])
Expand All @@ -146,20 +150,22 @@ def _http_post(cls, url, data):
def _http_post_dict(cls, url, data):
return cls._http_post(url, json.dumps(data, ensure_ascii=False))

def get_wechat_access_token(self):
if datetime.datetime.now() >= self.access_token_expire:
res = self._http_get(
@classmethod
def get_wechat_access_token(cls):
if datetime.datetime.now() >= cls.access_token_expire:
print("appid=%s secret=%s" %(cls.appid, cls.secret))
res = cls._http_get(
'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s' % (
self.appid, self.secret
cls.appid, cls.secret
)
)
rjson = json.loads(res)
if rjson.get('errcode'):
raise WeChatError(rjson['errcode'], rjson['errmsg'])
self.access_token = rjson['access_token']
self.access_token_expire = datetime.datetime.now() + datetime.timedelta(seconds=rjson['expires_in'] - 300)
self.logger.info('Got access token %s', self.access_token)
return self.access_token
cls.access_token = rjson['access_token']
cls.access_token_expire = datetime.datetime.now() + datetime.timedelta(seconds=rjson['expires_in'] - 300)
cls.logger.info('Got access token %s', cls.access_token)
return cls.access_token

def get_wechat_menu(self):
res = self._http_get(
Expand Down