Skip to content

Commit e0423bd

Browse files
committed
add a pre_request hook for weibo
1 parent 7cbabe7 commit e0423bd

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

example/weibo.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@
2626
def index():
2727
if 'oauth_token' in session:
2828
access_token = session['oauth_token'][0]
29-
# weibo is a shit !!!! It cannot be authorized by Bearer Token.
30-
resp = weibo.get('statuses/home_timeline.json', data={
31-
'access_token': access_token
32-
})
29+
resp = weibo.get('statuses/home_timeline.json')
3330
return jsonify(resp.data)
3431
return redirect(url_for('login'))
3532

@@ -64,5 +61,17 @@ def get_weibo_oauth_token():
6461
return session.get('oauth_token')
6562

6663

64+
def change_weibo_header(uri, headers, body):
65+
"""Since weibo is a rubbish server, it does not follow the standard,
66+
we need to change the authorization header for it."""
67+
auth = headers.get('Authorization')
68+
if auth:
69+
auth = auth.replace('Bearer', 'OAuth2')
70+
headers['Authorization'] = auth
71+
return uri, headers, body
72+
73+
weibo.pre_request = change_weibo_header
74+
75+
6776
if __name__ == '__main__':
6877
app.run()

flask_oauthlib/client.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,13 @@ def request(self, url, data=None, headers=None, format='urlencoded',
321321
uri, headers, body = client.add_token(
322322
url, http_method=method, body=data, headers=headers
323323
)
324+
325+
if hasattr(self, 'pre_request'):
326+
# this is desgined for some rubbish serice like weibo
327+
# since they don't follow the standards, we need to
328+
# change the uri, headers, or body
329+
uri, headers, body = self.pre_request(uri, headers, body)
330+
324331
resp, content = make_request(
325332
uri, headers, data=body, method=method
326333
)

0 commit comments

Comments
 (0)