本文整理汇总了Python中flickrapi.tokencache.TokenCache.forget方法的典型用法代码示例。如果您正苦于以下问题:Python TokenCache.forget方法的具体用法?Python TokenCache.forget怎么用?Python TokenCache.forget使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类flickrapi.tokencache.TokenCache
的用法示例。
在下文中一共展示了TokenCache.forget方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TokenCacheTest
# 需要导入模块: from flickrapi.tokencache import TokenCache [as 别名]
# 或者: from flickrapi.tokencache.TokenCache import forget [as 别名]
class TokenCacheTest(unittest.TestCase):
def setUp(self):
import tempfile
from flickrapi.tokencache import TokenCache
# Use mkdtemp() for backward compatibility with Python 2.7
self.tc_path = tempfile.mkdtemp()
self.tc = TokenCache('123-456', path=self.tc_path)
def tearDown(self):
tc_path = getattr(self, 'tc_path', None)
if tc_path is not None:
import shutil
shutil.rmtree(self.tc_path)
def test_get_set_del(self):
self.assertIsNone(self.tc.token)
# Check setting token, both in memory and on disk.
self.tc.token = u'nümbér'
self.assertEquals(self.tc.token, u'nümbér')
on_disk = open(self.tc.get_cached_token_filename(), 'rb').read()
self.assertEquals(on_disk.decode('utf8'), u'nümbér')
# Erase from in-RAM cache and try again, to read from disk.
self.tc.memory.clear()
self.assertEquals(self.tc.token, u'nümbér')
del self.tc.token
self.assertIsNone(self.tc.token)
self.tc.token = u'nümbér'
self.tc.forget()
self.assertIsNone(self.tc.token)
def test_username(self):
"""Username should impact the location of the cache on disk."""
from flickrapi.tokencache import TokenCache
user_tc = TokenCache(u'123-456', username=u'frøbel', path=self.tc_path)
tc_path = self.tc.get_cached_token_filename()
user_path = user_tc.get_cached_token_filename()
self.assertNotEquals(tc_path, user_path)
self.assertNotIn(u'frøbel', tc_path)
self.assertIn(u'frøbel', user_path)
示例2: FlickrAPI
# 需要导入模块: from flickrapi.tokencache import TokenCache [as 别名]
# 或者: from flickrapi.tokencache.TokenCache import forget [as 别名]
#.........这里部分代码省略.........
- Wait for the user to signal the program that the
authorization was performed, but only if there was no
cached token.
- Call flickrapi.get_token_part_two(...) and pass it the
result value you stored.
The newly minted token is then cached locally for the next
run.
perms
"read", "write", or "delete"
auth_callback
method to be called if authorization is needed. When not
passed, ``self.validate_frob(...)`` is called. You can
call this method yourself from the callback method too.
If authorization should be blocked, pass
``auth_callback=False``.
The auth_callback method should take ``(frob, perms)`` as
parameters.
An example::
(token, frob) = flickr.get_token_part_one(perms='write')
if not token: raw_input("Press ENTER after you authorized this program")
flickr.get_token_part_two((token, frob))
Also take a look at ``authenticate_console(perms)``.
"""
# Check our auth_callback parameter for correctness before we
# do anything
authenticate = self.validate_frob
if auth_callback is not None:
if hasattr(auth_callback, '__call__'):
# use the provided callback function
authenticate = auth_callback
elif auth_callback is False:
authenticate = None
else:
# Any non-callable non-False value is invalid
raise ValueError('Invalid value for auth_callback: %s'
% auth_callback)
# see if we have a saved token
token = self.token_cache.token
frob = None
auth_url = None
nsid = None
# see if it's valid
if token:
LOG.debug("Trying cached token '%s'" % token)
try:
rsp = self.auth_checkToken(auth_token=token, format='xmlnode')
# see if we have enough permissions
tokenPerms = rsp.auth[0].perms[0].text
if tokenPerms == "read" and perms != "read": token = None
elif tokenPerms == "write" and perms == "delete": token = None
if token:
nsid = rsp.auth[0].user[0]['nsid']
except FlickrError:
LOG.debug("Cached token invalid")
self.token_cache.forget()
token = None
# get a new token if we need one
if not token:
# If we can't authenticate, it's all over.
if not authenticate:
raise FlickrError('Authentication required but '
'blocked using auth_callback=False')
# get the frob
LOG.debug("Getting frob for new token")
rsp = self.auth_getFrob(auth_token=None, format='xmlnode')
frob = rsp.frob[0].text
#authenticate(frob, perms)
auth_url = self.auth_url(perms, frob)
return (token, frob, nsid, auth_url)
def get_token_part_two(self, (token, frob)):
"""Part two of getting a token, see ``get_token_part_one(...)`` for details."""
# If a valid token was obtained in the past, we're done
if token:
LOG.debug("get_token_part_two: no need, token already there")
self.token_cache.token = token
return token
LOG.debug("get_token_part_two: getting a new token for frob '%s'" % frob)
return self.get_token(frob)
示例3: __init__
# 需要导入模块: from flickrapi.tokencache import TokenCache [as 别名]
# 或者: from flickrapi.tokencache.TokenCache import forget [as 别名]
#.........这里部分代码省略.........
if rsp['stat'] == "fail":
return rsp.err[0]['msg']
return "Success"
test_failure = classmethod(test_failure)
get_printable_error = classmethod(get_printable_error)
get_rsp_error_code = classmethod(get_rsp_error_code)
get_rsp_error_msg = classmethod(get_rsp_error_msg)
def validate_frob(self, frob, perms):
'''Lets the user validate the frob by launching a browser to
the Flickr website.
'''
auth_url = self.auth_url(perms, frob)
webbrowser.open(auth_url, True, True)
def get_token_part_one(self, perms="read"):
"""Get a token either from the cache, or make a new one from
the frob.
This first attempts to find a token in the user's token cache
on disk. If that token is present and valid, it is returned by
the method.
If that fails (or if the token is no longer valid based on
flickr.auth.checkToken) a new frob is acquired. The frob is
validated by having the user log into flickr (with a browser).
To get a proper token, follow these steps:
- Store the result value of this method call
- Give the user a way to signal the program that he/she
has authorized it, for example show a button that can be
pressed.
- Wait for the user to signal the program that the
authorization was performed, but only if there was no
cached token.
- Call flickrapi.get_token_part_two(...) and pass it the
result value you stored.
The newly minted token is then cached locally for the next
run.
perms
"read", "write", or "delete"
An example::
(token, frob) = flickr.get_token_part_one(perms='write')
if not token: raw_input("Press ENTER after you authorized this program")
flickr.get_token_part_two((token, frob))
"""
# see if we have a saved token
token = self.token_cache.token
frob = None
# see if it's valid
if token:
LOG.debug("Trying cached token '%s'" % token)
try:
rsp = self.auth_checkToken(auth_token=token)
# see if we have enough permissions
tokenPerms = rsp.auth[0].perms[0].text
self.username = rsp.auth[0].user[0]['username']
if tokenPerms == "read" and perms != "read": token = None
elif tokenPerms == "write" and perms == "delete": token = None
except FlickrError:
LOG.debug("Cached token invalid")
self.token_cache.forget()
token = None
# get a new token if we need one
if not token:
# get the frob
LOG.debug("Getting frob for new token")
rsp = self.auth_getFrob(auth_token=None)
self.test_failure(rsp)
frob = rsp.frob[0].text
# validate online
self.validate_frob(frob, perms)
return (token, frob)
def get_token_part_two(self, (token, frob)):
"""Part two of getting a token, see ``get_token_part_one(...)`` for details."""
# If a valid token was obtained in the past, we're done
if token:
LOG.debug("get_token_part_two: no need, token already there")
self.token_cache.token = token
return token
LOG.debug("get_token_part_two: getting a new token for frob '%s'" % frob)
return self.get_token(frob)
示例4: FlickrAPI
# 需要导入模块: from flickrapi.tokencache import TokenCache [as 别名]
# 或者: from flickrapi.tokencache.TokenCache import forget [as 别名]
#.........这里部分代码省略.........
#-----------------------------------------------------------------------
def validateFrob(self, frob, perms):
auth_url = self.__getAuthURL(perms, frob)
webbrowser.open(auth_url, True, True)
#-----------------------------------------------------------------------
def getTokenPartOne(self, perms="read"):
"""Get a token either from the cache, or make a new one from the
frob.
This first attempts to find a token in the user's token cache on
disk. If that token is present and valid, it is returned by the
method.
If that fails (or if the token is no longer valid based on
flickr.auth.checkToken) a new frob is acquired. The frob is
validated by having the user log into flickr (with a browser).
If the browser needs to take over the terminal, use fork=False,
otherwise use fork=True.
To get a proper token, follow these steps:
- Store the result value of this method call
- Give the user a way to signal the program that he/she has
authorized it, for example show a button that can be
pressed.
- Wait for the user to signal the program that the
authorization was performed, but only if there was no
cached token.
- Call flickrapi.getTokenPartTwo(...) and pass it the result
value you stored.
The newly minted token is then cached locally for the next run.
perms--"read", "write", or "delete"
An example:
(token, frob) = flickr.getTokenPartOne(perms='write')
if not token: raw_input("Press ENTER after you authorized this program")
flickr.getTokenPartTwo((token, frob))
"""
# see if we have a saved token
token = self.token_cache.token
frob = None
# see if it's valid
if token:
LOG.debug("Trying cached token '%s'" % token)
try:
rsp = self.auth_checkToken(api_key=self.apiKey, auth_token=token)
# see if we have enough permissions
tokenPerms = rsp.auth[0].perms[0].elementText
if tokenPerms == "read" and perms != "read": token = None
elif tokenPerms == "write" and perms == "delete": token = None
except FlickrError:
LOG.debug("Cached token invalid")
self.token_cache.forget()
token = None
self.token = None
# get a new token if we need one
if not token:
# get the frob
LOG.debug("Getting frob for new token")
rsp = self.auth_getFrob(api_key=self.apiKey, auth_token=None)
self.testFailure(rsp)
frob = rsp.frob[0].elementText
# validate online
self.validateFrob(frob, perms)
return (token, frob)
def getTokenPartTwo(self, (token, frob)):
"""Part two of getting a token, see getTokenPartOne(...) for details."""
# If a valid token was obtained, we're done
if token:
LOG.debug("getTokenPartTwo: no need, token already there")
self.token = token
return token
LOG.debug("getTokenPartTwo: getting a new token for frob '%s'" % frob)
# get a token
rsp = self.auth_getToken(api_key=self.apiKey, frob=frob)
self.testFailure(rsp)
token = rsp.auth[0].token[0].elementText
LOG.debug("getTokenPartTwo: new token '%s'" % token)
# store the auth info for next time
self.token_cache.token = rsp.xml
self.token = token
return token
示例5: __init__
# 需要导入模块: from flickrapi.tokencache import TokenCache [as 别名]
# 或者: from flickrapi.tokencache.TokenCache import forget [as 别名]
#.........这里部分代码省略.........
if no error.
"""
LOG.warn("FlickrAPI.get_rsp_error_msg has been deprecated and will be "
"removed in FlickrAPI version 1.2.")
if rsp['stat'] == "fail":
return rsp.err[0]['msg']
return "Success"
def validate_frob(self, frob, perms):
'''Lets the user validate the frob by launching a browser to
the Flickr website.
'''
auth_url = self.auth_url(perms, frob)
webbrowser.open(auth_url, True, True)
def get_token_part_one(self, perms="read"):
"""Get a token either from the cache, or make a new one from
the frob.
This first attempts to find a token in the user's token cache
on disk. If that token is present and valid, it is returned by
the method.
If that fails (or if the token is no longer valid based on
flickr.auth.checkToken) a new frob is acquired. The frob is
validated by having the user log into flickr (with a browser).
To get a proper token, follow these steps:
- Store the result value of this method call
- Give the user a way to signal the program that he/she
has authorized it, for example show a button that can be
pressed.
- Wait for the user to signal the program that the
authorization was performed, but only if there was no
cached token.
- Call flickrapi.get_token_part_two(...) and pass it the
result value you stored.
The newly minted token is then cached locally for the next
run.
perms
"read", "write", or "delete"
An example::
(token, frob) = flickr.get_token_part_one(perms='write')
if not token: raw_input("Press ENTER after you authorized this program")
flickr.get_token_part_two((token, frob))
"""
# see if we have a saved token
token = self.token_cache.token
frob = None
# see if it's valid
if token:
LOG.debug("Trying cached token '%s'" % token)
try:
rsp = self.auth_checkToken(auth_token=token, format='xmlnode')
# see if we have enough permissions
tokenPerms = rsp.auth[0].perms[0].text
if tokenPerms == "read" and perms != "read": token = None
elif tokenPerms == "write" and perms == "delete": token = None
except FlickrError:
LOG.debug("Cached token invalid")
self.token_cache.forget()
token = None
# get a new token if we need one
if not token:
# get the frob
LOG.debug("Getting frob for new token")
rsp = self.auth_getFrob(auth_token=None, format='xmlnode')
self.test_failure(rsp)
frob = rsp.frob[0].text
# validate online
self.validate_frob(frob, perms)
return (token, frob)
def get_token_part_two(self, (token, frob)):
"""Part two of getting a token, see ``get_token_part_one(...)`` for details."""
# If a valid token was obtained in the past, we're done
if token:
LOG.debug("get_token_part_two: no need, token already there")
self.token_cache.token = token
return token
LOG.debug("get_token_part_two: getting a new token for frob '%s'" % frob)
return self.get_token(frob)