本文整理汇总了Python中weibo.APIClient.is_expires方法的典型用法代码示例。如果您正苦于以下问题:Python APIClient.is_expires方法的具体用法?Python APIClient.is_expires怎么用?Python APIClient.is_expires使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类weibo.APIClient
的用法示例。
在下文中一共展示了APIClient.is_expires方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: new_view
# 需要导入模块: from weibo import APIClient [as 别名]
# 或者: from weibo.APIClient import is_expires [as 别名]
def new_view(request, *args, **kwargs):
if 'token' in request.session and 'expire' in request.session:
client = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL)
client.set_access_token(request.session['token'], request.session['expire'])
if client.is_expires():
return render(request, 'loginForm.html')
return view(request, client, *args, **kwargs)
return render(request, 'loginForm.html')
示例2: weiboNoAuthClientTest
# 需要导入模块: from weibo import APIClient [as 别名]
# 或者: from weibo.APIClient import is_expires [as 别名]
class weiboNoAuthClientTest(unittest.TestCase):
def setUp(self):
self.client = APIClient()
self.app_key = "1865902151"
def test_isExpired(self):
self.assertFalse(self.client.is_expires())
def test_format(self):
self.assertEqual(self.client.format, "json")
def test_apitype(self):
self.assertEqual(self.client.api_url, "https://api.weibo.com/2/")
示例3: index
# 需要导入模块: from weibo import APIClient [as 别名]
# 或者: from weibo.APIClient import is_expires [as 别名]
def index(request):
client = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL)
requestResults = dict()
if "code" in request.GET:
print("in request code, set_cookie")
code = request.GET['code']
r = client.request_access_token(code)
access_token = r.access_token
expires_in = r.expires_in
client.set_access_token(access_token, expires_in)
elif "access_token" in request.COOKIES:
print("has access_token in cookies")
access_token = request.COOKIES["access_token"]
expires_in = request.COOKIES["expires_in"]
client.set_access_token(access_token, expires_in)
if not client.is_expires():
print("client is not expires, get information")
accountUid = client.get.account__get_uid()
usersShow = client.get.users__show(uid=accountUid["uid"])
requestResults["usersShow"] = usersShow
else:
print("client is expire, authorize again")
authorizeUrl = client.get_authorize_url()
requestResults["authorizeUrl"] = authorizeUrl
response = render_to_response("amour/index.html", requestResults)
if client.is_expires():
print("client is expires, clear cookie")
response.delete_cookie("access_token")
response.delete_cookie("expires_in")
else:
print("client is not expires, set cooke")
response.set_cookie("access_token", access_token)
response.set_cookie('expires_in', expires_in)
return response
示例4: weiboAPI
# 需要导入模块: from weibo import APIClient [as 别名]
# 或者: from weibo.APIClient import is_expires [as 别名]
class weiboAPI(object):
'''
该类是线程安全的,特别是需要使用weibo链接网络的方法一定要加锁,还有就是判断访问频次的方法也要加锁
如果要用户授权,要保证授权未过期!!
'''
def __init__(self, access_token=None, expires_in=None, u_id=None):
self.client = APIClient(app_key=APP_KEY,
app_secret=APP_SECRET,
redirect_uri=CALLBACK_URL,
)
self.u_id = u_id
self.REMIND_WEIBO_ID = 3504267275499498
self.lock = threading.Lock() # #多线程同步
self.acc_count = {}
self.acc_limit = {}
self._initAccessCount()
self._initAccessLimit()
if access_token is not None and expires_in is not None:
self.client.set_access_token(access_token, expires_in)
self.refreshAccessToken()
logger.info('current user:' + str(u_id) + \
'\naccess_token:' + str(access_token) + \
'\nexpires_in' + str(expires_in))
else:
logger.info('just weibo client, no user authorized')
def _initAccessCount(self):
'''
该方法没有加锁,要求调用的方法必须是线程安全的
'''
self.acc_count['hour'] = time.localtime().tm_hour
self.acc_count['ip_all'] = 0 # 单小时限制:10000
self.acc_count['user_all'] = 0 # 单小时限制:1000
self.acc_count['user_status'] = 0 # 单小时限制:30
self.acc_count['user_comment'] = 0 # 单小时限制:60
self.acc_count['user_follow'] = 0 # 单小时限制:60;这个还有每天的限制,没有考虑
logger.info('_initAccessCount:\t' + str(self.acc_count))
def _initAccessLimit(self):
'''
该方法没有加锁,要求调用的方法必须是线程安全的
'''
self.acc_limit['time_unit'] = 'hour'
self.acc_limit['ip_all'] = 10000 - 100 # #减去这些值是为了考虑到有漏掉的访问
self.acc_limit['user_all'] = 1000 - 20
self.acc_limit['user_status'] = 30 - 5
self.acc_limit['user_comment'] = 60 - 10
self.acc_limit['user_follow'] = 60 - 10 # 这个还有每天的限制,没有考虑
logger.info('_initAccessLimit:\t' + str(self.acc_limit))
def _checkAccessLimit(self, type='ip_all'):
'''
线程安全
'''
with self.lock:
logger.info('_checkAccessLimit, type:' + str(type))
logger.info('old acc_count:\t' + str(self.acc_count))
# #MARK: 两小时刷新一次记录会不会过分了
if self.acc_count['hour'] < time.localtime().tm_hour or \
self.acc_count['hour'] > time.localtime().tm_hour + 1:
self._initAccessCount()
else:
self.acc_count[type] += 1
if type != 'ip_all':
self.acc_count['ip_all'] += 1
self.acc_count['user_all'] += 1
logger.info('new acc_count:\t' + str(self.acc_count))
if self.acc_count[type] > self.acc_limit[type] or \
self.acc_count['ip_all'] > self.acc_limit['ip_all'] or \
self.acc_count['user_all'] > self.acc_limit['user_all']:
sleeptime = (60 - time.localtime().tm_min + 65) * 60 # #MARK目前多睡1小时。。。
logger.info('access limit reached, sleep for ' + str(sleeptime / 60) + ' minutes')
time.sleep(sleeptime)
def refreshAccessToken(self):
'''
这个方法实现自动授权?
'''
logger.info('refreshAccessToken')
if self.client.is_expires():
logger.error('refreshAccessToken: access_token is expired')
self._setAccessTokenManually()
def _setAccessTokenManually(self):
url = self.client.get_authorize_url()
print 'weibo.refreshAccessToken 访问url获取认证code\n', url
# TODO:能否impl自动认证
code = raw_input()
r = self.client.request_access_token(code)
access_token = r.access_token # 新浪返回的token,类似abc123xyz456
expires_in = r.expires_in # token过期的UNIX时间:http://zh.wikipedia.org/wiki/UNIX%E6%97%B6%E9%97%B4
self.client.set_access_token(access_token, expires_in)
#.........这里部分代码省略.........
示例5: APIClient
# 需要导入模块: from weibo import APIClient [as 别名]
# 或者: from weibo.APIClient import is_expires [as 别名]
__author__ = 'zice'
from weibo import APIClient
APP_KEY = '2038546250'
APP_SECRET = '24bb05a6fb21eadf36713d75f2fe2b77'
CALLBACK_URL = 'http://www.example.com/callback'
code = '8067a19c36a182b6643d77825ccc3ab4'
client = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL)
r = client.request_access_token(code)
access_token = r.access_token
expires_in = r.expires_in
print access_token
print expires_in
client.set_access_token(access_token, expires_in)
s = client.is_expires()
print s