def get_token(self, token_only=True, scopes=None):
if scopes is None:
scopes = ['send_notification', 'view_room']
cache_key = 'hipchat-tokens:%s:%s' % (self.id, ','.join(scopes))
def gen_token():
data = {
'grant_type': 'client_credentials',
'scope': ' '.join(scopes),
}
resp = requests.post(
self.token_url, data=data, auth=HTTPBasicAuth(self.id, self.secret), timeout=10
)
if resp.status_code == 200:
return resp.json()
elif resp.status_code == 401:
raise OauthClientInvalidError(self)
else:
raise Exception('Invalid token: %s' % resp.text)
if token_only:
token = cache.get(cache_key)
if not token:
data = gen_token()
token = data['access_token']
cache.set(cache_key, token, data['expires_in'] - 20)
return token
return gen_token()
评论列表
文章目录