def get_access_token(self, renew=False):
""" Get an access token using your app_id and app_secret.
You shouldn't need to call this method yourself. If there is no access token yet, this method
will be called when a request is made. If a token expires, this method will also automatically
be called to renew the token.
Args:
renew: if True, then force the client to get a new token (even if not expired). By default if
there is already an access token in the client then this method is a no-op.
"""
if self.access_token is None or renew:
headers = {} # don't use json here, juse urlencode.
url = self._url_for_op('token')
data = urlencode({'grant_type': 'client_credentials',
'client_id':self.CLIENT_ID,
'client_secret':self.CLIENT_SECRET})
data = bytearray(data, 'utf-8')
req = urllib2.Request(url, data, headers)
try:
response = urllib2.urlopen(req).read()
response = self._parse_response(response)
except urllib2.HTTPError as e:
raise ApiError(e.reason)
except Exception as e:
raise ApiError(e)
self.access_token = response['access_token']
return self.access_token
评论列表
文章目录