From 257c318c2f11618eb4ab56fda5362cabe3e45af2 Mon Sep 17 00:00:00 2001 From: kousu Date: Tue, 5 Jan 2016 00:24:01 -0500 Subject: [PATCH] Fail-fast on server errors. Most OAuth providers give HTTP error codes if something goes wrong. Call raise_for_status() instead of failing with MissingTokenError. The call is *after* the blocks of debug statements so that you can still figure out what went wrong, if you know where to look. --- requests_oauthlib/oauth2_session.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/requests_oauthlib/oauth2_session.py b/requests_oauthlib/oauth2_session.py index 9aa2e02b..57a6ebdf 100644 --- a/requests_oauthlib/oauth2_session.py +++ b/requests_oauthlib/oauth2_session.py @@ -229,6 +229,9 @@ def fetch_token(self, token_url, code=None, authorization_response=None, r.headers, r.text) log.debug('Invoking %d token response hooks.', len(self.compliance_hook['access_token_response'])) + + r.raise_for_status() + for hook in self.compliance_hook['access_token_response']: log.debug('Invoking hook %s.', hook) r = hook(r) @@ -296,6 +299,9 @@ def refresh_token(self, token_url, refresh_token=None, body='', auth=None, r.headers, r.text) log.debug('Invoking %d token response hooks.', len(self.compliance_hook['refresh_token_response'])) + + r.raise_for_status() + for hook in self.compliance_hook['refresh_token_response']: log.debug('Invoking hook %s.', hook) r = hook(r)