Skip to content

Commit 4cd5642

Browse files
krrrlepture
authored andcommitted
Fix qq example for Py3k (lepture#242)
1 parent 63ad953 commit 4cd5642

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

example/qq.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from flask import Flask, redirect, url_for, session, request, jsonify, Markup
55
from flask_oauthlib.client import OAuth
66

7+
# get yours at http://connect.qq.com
78
QQ_APP_ID = os.getenv('QQ_APP_ID', '101187283')
89
QQ_APP_KEY = os.getenv('QQ_APP_KEY', '993983549da49e384d03adfead8b2489')
910

@@ -25,13 +26,17 @@
2526

2627

2728
def json_to_dict(x):
28-
'''OAuthResponse class can't not parse the JSON data with content-type
29-
text/html, so we need reload the JSON data manually'''
30-
if x.find('callback') > -1:
31-
pos_lb = x.find('{')
32-
pos_rb = x.find('}')
29+
'''OAuthResponse class can't parse the JSON data with content-type
30+
- text/html and because of a rubbish api, we can't just tell flask-oauthlib to treat it as json.'''
31+
if x.find(b'callback') > -1:
32+
# the rubbish api (https://graph.qq.com/oauth2.0/authorize) is handled here as special case
33+
pos_lb = x.find(b'{')
34+
pos_rb = x.find(b'}')
3335
x = x[pos_lb:pos_rb + 1]
36+
3437
try:
38+
if type(x) != str: # Py3k
39+
x = x.decode('utf-8')
3540
return json.loads(x, encoding='utf-8')
3641
except:
3742
return x
@@ -60,7 +65,7 @@ def get_user_info():
6065
if 'qq_token' in session:
6166
data = update_qq_api_request_data()
6267
resp = qq.get('/user/get_user_info', data=data)
63-
return jsonify(status=resp.status, data=resp.data)
68+
return jsonify(status=resp.status, data=json_to_dict(resp.data))
6469
return redirect(url_for('login'))
6570

6671

0 commit comments

Comments
 (0)