Skip to content

Commit c0a0b9c

Browse files
committed
重构Token
1 parent 3672338 commit c0a0b9c

File tree

5 files changed

+8
-97
lines changed

5 files changed

+8
-97
lines changed

chinaapi/douban/open.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
# coding=utf-8
22
from chinaapi.exceptions import ApiResponseError
3-
from chinaapi.open import OAuth2Base, Token as TokenBase, App
4-
5-
6-
class Token(TokenBase):
7-
"""
8-
douban_user_id:豆瓣用户ID
9-
"""
10-
11-
def __init__(self, access_token=None, expires_in=None, refresh_token=None, **kwargs):
12-
super(Token, self).__init__(access_token, expires_in, refresh_token, **kwargs)
13-
self.douban_user_id = kwargs.pop('douban_user_id', None)
3+
from chinaapi.open import OAuth2Base, Token, App
144

155

166
class OAuth2(OAuth2Base):

chinaapi/qq/weibo/open.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# coding=utf-8
2-
from chinaapi.open import ClientBase, Method, OAuth2Base, Token as TokenBase, App
2+
from chinaapi.open import ClientBase, Method, OAuth2Base, Token, App
33
from chinaapi.exceptions import InvalidApi, ApiResponseError
44
from chinaapi.utils import parse_querystring
55

@@ -37,19 +37,6 @@ def parse(response):
3737
return r
3838

3939

40-
class Token(TokenBase):
41-
"""
42-
openid:用户统一标识,可以唯一标识一个用户
43-
openkey:与openid对应的用户key,是验证openid身份的验证密钥
44-
"""
45-
46-
def __init__(self, access_token=None, expires_in=None, refresh_token=None, **kwargs):
47-
super(Token, self).__init__(access_token, expires_in, refresh_token, **kwargs)
48-
self.openid = kwargs.pop('openid', None)
49-
self.openkey = kwargs.pop('openkey', None)
50-
self.name = kwargs.pop('name', None)
51-
52-
5340
class Client(ClientBase):
5441
#写接口
5542
_post_methods = ['add', 'del', 'create', 'delete', 'update', 'upload']

chinaapi/renren/open.py

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# coding=utf-8
2-
from chinaapi.open import ClientBase, Method, OAuth2Base, Token as TokenBase, App
2+
from chinaapi.open import ClientBase, Method, OAuth2Base, Token, App
33
from chinaapi.exceptions import ApiResponseError
44

55

@@ -29,31 +29,6 @@ def _parse_response(self, response):
2929
return r
3030

3131

32-
class Token(TokenBase):
33-
"""
34-
token_type:Token类型,bearer或者mac
35-
scope:Access Token最终的访问范围,既用户实际授予的权限列表
36-
user:用户的个人信息,包含用户id,名称“name”,头像“avatar”(
37-
包含四种大小不同的尺寸“type”,大小依次为:“tiny”,“avatar”,“main”,“large”)
38-
mac_algorithm:当token_type参数为mac时返回该值
39-
mac_key:当token_type参数为mac时返回该值
40-
"""
41-
42-
class User(object):
43-
def __init__(self, id=None, name=None, avatar=None):
44-
self.id = id
45-
self.name = name
46-
self.avatar = avatar
47-
48-
def __init__(self, access_token=None, expires_in=None, refresh_token=None, **kwargs):
49-
super(Token, self).__init__(access_token, expires_in, refresh_token, **kwargs)
50-
self.token_type = kwargs.pop('token_type', None)
51-
self.scope = kwargs.pop('scope', None)
52-
self.user = kwargs.pop('user', None)
53-
self.mac_algorithm = kwargs.pop('mac_algorithm', None)
54-
self.mac_key = kwargs.pop('mac_key', None)
55-
56-
5732
class OAuth2(OAuth2Base):
5833
AUTH_URL = 'https://graph.renren.com/oauth/authorize'
5934
TOKEN_URL = 'https://graph.renren.com/oauth/token'
@@ -65,7 +40,6 @@ def _parse_token(self, response):
6540
r = response.json_dict()
6641
if 'error_code' in r:
6742
raise ApiResponseError(response, r.error_code, r.get('error_description', r.get('error', '')))
68-
r['user'] = Token.User(**r.pop('user'))
6943
return Token(**r)
7044

7145

chinaapi/sina/weibo/open.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import hashlib
44
import hmac
55
from chinaapi.utils import parse_querystring
6-
from chinaapi.open import ClientBase, Method, OAuth2Base, Token as TokenBase, App
6+
from chinaapi.open import ClientBase, Method, OAuth2Base, Token, App
77
from chinaapi.exceptions import ApiResponseError
88
from chinaapi.jsonDict import loads
99

@@ -71,18 +71,6 @@ def _is_retry_error(self, e):
7171
return e.code in RETRY_CODES
7272

7373

74-
class Token(TokenBase):
75-
"""
76-
uid:授权用户的uid
77-
created_at:令牌创建日期,为timestamp格式
78-
"""
79-
80-
def __init__(self, access_token=None, expires_in=None, refresh_token=None, **kwargs):
81-
super(Token, self).__init__(access_token, expires_in, refresh_token, **kwargs)
82-
self.uid = kwargs.pop('uid', None)
83-
self.created_at = kwargs.pop('created_at', None)
84-
85-
8674
class OAuth2(OAuth2Base):
8775
BASE_URL = 'https://api.weibo.com/oauth2/'
8876
AUTH_URL = BASE_URL + 'authorize'
@@ -93,9 +81,9 @@ def __init__(self, app):
9381

9482
def _parse_token(self, response):
9583
data = parse(response)
96-
data['created_at'] = data.pop('create_at', None)
84+
data['created_at'] = data.get('create_at', None)
9785
if 'expires_in' not in data:
98-
data['expires_in'] = data.pop('expire_in', None)
86+
data['expires_in'] = data.get('expire_in', None)
9987
return Token(**data)
10088

10189
def revoke(self, access_token):
@@ -127,7 +115,7 @@ def base64decode(s):
127115
encoded_sign, encoded_data = signed_request.split('.', 1)
128116
sign = base64decode(encoded_sign)
129117
data = loads(base64decode(encoded_data))
130-
token = Token(data.oauth_token, data.expires, uid=data.user_id, created_at=data.issued_at)
118+
token = Token(data.oauth_token, data.expires, uid=data.user_id, created_at=data.issued_at, **data)
131119
is_valid = data.algorithm == u'HMAC-SHA256' and hmac.new(self.app.key, encoded_data,
132120
hashlib.sha256).digest() == sign
133121
return token, is_valid

chinaapi/taobao/open.py

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from hashlib import md5
55
from datetime import datetime
66
from urllib import unquote
7-
from chinaapi.open import ClientBase, OAuthBase, OAuth2Base, App, Token as TokenBase
7+
from chinaapi.open import ClientBase, OAuthBase, OAuth2Base, App, Token
88
from chinaapi.exceptions import ApiResponseError
99
from chinaapi.utils import parse_querystring
1010

@@ -94,34 +94,6 @@ def _is_retry_error(self, e):
9494
return e.sub_code in RETRY_SUB_CODES
9595

9696

97-
class Token(TokenBase):
98-
"""
99-
taobao_user_nick:淘宝账号
100-
taobao_user_id:淘宝帐号对应id
101-
sub_taobao_user_nick:淘宝子账号
102-
sub_taobao_user_id:淘宝子账号对应id
103-
token_type:Access token的类型目前只支持bearer
104-
re_expires_in:Refresh token过期时间
105-
r1_expires_in:r1级别API或字段的访问过期时间
106-
r2_expires_in:r2级别API或字段的访问过期时间
107-
w1_expires_in:w1级别API或字段的访问过期时间
108-
w2_expires_in:w2级别API或字段的访问过期时间
109-
"""
110-
111-
def __init__(self, access_token=None, expires_in=None, refresh_token=None, **kwargs):
112-
super(Token, self).__init__(access_token, expires_in, refresh_token, **kwargs)
113-
self.taobao_user_nick = kwargs.pop('taobao_user_nick', None)
114-
self.taobao_user_id = kwargs.pop('taobao_user_id', None)
115-
self.sub_taobao_user_nick = kwargs.pop('sub_taobao_user_nick', None)
116-
self.sub_taobao_user_id = kwargs.pop('sub_taobao_user_id', None)
117-
self.token_type = kwargs.pop('token_type', None)
118-
self.re_expires_in = kwargs.pop('re_expires_in', None)
119-
self.r1_expires_in = kwargs.pop('r1_expires_in', None)
120-
self.r2_expires_in = kwargs.pop('r2_expires_in', None)
121-
self.w1_expires_in = kwargs.pop('w1_expires_in', None)
122-
self.w2_expires_in = kwargs.pop('w2_expires_in', None)
123-
124-
12597
def parse(response):
12698
r = response.json_dict()
12799
if 'error' in r:

0 commit comments

Comments
 (0)