-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdemo.py
61 lines (49 loc) · 1.98 KB
/
demo.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import json
import os
from builtins import input
from flask import Flask
from flask_mwoauth import MWOAuth
app = Flask(__name__)
# Generate a random secret application key
#
# NOTE: this key changes every invocation. In an actual application, the key
# should not change! Otherwise you might get a different secret key for
# different requests, which means you can't read data stored in cookies,
# which in turn breaks OAuth.
#
# So, for an actual application, use app.secret_key = "some long secret key"
# (which you could generate using os.urandom(24))
#
app.secret_key = os.urandom(24)
print("""
NOTE: The callback URL you entered when proposing an OAuth consumer
probably did not match the URL under which you are running this development
server. Your redirect back will therefore fail -- please adapt the URL in
your address bar to http://localhost:5000/oauth-callback?oauth_verifier=...etc
""")
try:
creds_doc = json.load(open("credentials.do_not_commit.json"))
consumer_key = creds_doc['consumer_key']
consumer_secret = creds_doc['consumer_secret']
except FileNotFoundError:
print('Couldn\'t find "credentials.do_not_commit.json".' +
'Please manually input credentials.')
consumer_key = input('Consumer key: ')
consumer_secret = input('Consumer secret: ')
mwoauth = MWOAuth(consumer_key=consumer_key, consumer_secret=consumer_secret)
app.register_blueprint(mwoauth.bp)
@app.route("/")
def index():
return "logged in as: " + repr(mwoauth.get_current_user(True)) + \
"<br> <a href=login>login</a> / " + \
"<a href=test_query>test_query</a> / " + \
"<a href=logout>logout</a>"
@app.route("/test_query")
def test_query():
username = mwoauth.get_current_user(True)
data = mwoauth.request({'action': "query", "list": "usercontribs",
'ucuser': str(username), 'ucprop': "timestamp",
'format': "json"})
return data
if __name__ == "__main__":
app.run(debug=True, threaded=True)