-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth_wiki.py
42 lines (29 loc) · 1.11 KB
/
oauth_wiki.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from flask import current_app, session
from requests_oauthlib import OAuth1Session
from urllib.parse import urlencode
project = 'https://commons.wikimedia.org/w/api.php?'
def raw_request(params):
app = current_app
url = project + urlencode(params)
client_key = app.config['CONSUMER_KEY']
client_secret = app.config['CONSUMER_SECRET']
oauth = OAuth1Session(client_key,
client_secret=client_secret,
resource_owner_key=session['owner_key'],
resource_owner_secret=session['owner_secret'])
return oauth.get(url, timeout=4)
def api_request(params):
return raw_request(params).json()
def userinfo_call():
params = {'action': 'query', 'meta': 'userinfo', 'format': 'json'}
return api_request(params)
def get_username():
if 'owner_key' not in session:
return # not authorized
if 'username' in session:
return session['username']
reply = userinfo_call()
if 'query' not in reply:
return
session['username'] = reply['query']['userinfo']['name']
return session['username']