|
4 | 4 | from flask import Flask, redirect, url_for, session, request, jsonify, Markup |
5 | 5 | from flask_oauthlib.client import OAuth |
6 | 6 |
|
| 7 | +# get yours at http://connect.qq.com |
7 | 8 | QQ_APP_ID = os.getenv('QQ_APP_ID', '101187283') |
8 | 9 | QQ_APP_KEY = os.getenv('QQ_APP_KEY', '993983549da49e384d03adfead8b2489') |
9 | 10 |
|
|
25 | 26 |
|
26 | 27 |
|
27 | 28 | 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'}') |
33 | 35 | x = x[pos_lb:pos_rb + 1] |
| 36 | + |
34 | 37 | try: |
| 38 | + if type(x) != str: # Py3k |
| 39 | + x = x.decode('utf-8') |
35 | 40 | return json.loads(x, encoding='utf-8') |
36 | 41 | except: |
37 | 42 | return x |
@@ -60,7 +65,7 @@ def get_user_info(): |
60 | 65 | if 'qq_token' in session: |
61 | 66 | data = update_qq_api_request_data() |
62 | 67 | 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)) |
64 | 69 | return redirect(url_for('login')) |
65 | 70 |
|
66 | 71 |
|
|
0 commit comments