3 from urlparse import parse_qsl
5 from urllib.parse import parse_qsl
7 from oauthlib.common import to_unicode
10 def facebook_compliance_fix(session):
12 def _compliance_fix(r):
13 # if Facebook claims to be sending us json, let's trust them.
14 if 'application/json' in r.headers.get('content-type', {}):
17 # Facebook returns a content-type of text/plain when sending their
18 # x-www-form-urlencoded responses, along with a 200. If not, let's
19 # assume we're getting JSON and bail on the fix.
20 if 'text/plain' in r.headers.get('content-type', {}) and r.status_code == 200:
21 token = dict(parse_qsl(r.text, keep_blank_values=True))
25 expires = token.get('expires')
26 if expires is not None:
27 token['expires_in'] = expires
28 token['token_type'] = 'Bearer'
29 r._content = to_unicode(dumps(token)).encode('UTF-8')
32 session.register_compliance_hook('access_token_response', _compliance_fix)