Updated packages and code to python3. Won't work with python 2
[twitter-api-cdsw] / requests_oauthlib / compliance_fixes / facebook.py
1 from json import dumps
2 try:
3     from urlparse import parse_qsl
4 except ImportError:
5     from urllib.parse import parse_qsl
6
7 from oauthlib.common import to_unicode
8
9
10 def facebook_compliance_fix(session):
11
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', {}):
15             return r
16
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))
22         else:
23             return r
24
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')
30         return r
31
32     session.register_compliance_hook('access_token_response', _compliance_fix)
33     return session

Benjamin Mako Hill || Want to submit a patch?