-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonzo_client_py
executable file
·95 lines (76 loc) · 3.01 KB
/
monzo_client_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python
from flask import Flask, abort, request
from uuid import uuid4
#import requests
#import requests.auth
import urllib
CLIENT_ID = "oauthclient_00009Qs5klehAJyGUjKdjF"
CLIENT_SECRET = "JSkQ7mxVXwEJUgf0gAe0dcH9nZFEHyL+f1GxQlWs2yRK8heHWGy40FbOZ3WHrvZk/8qu/c09UydmTkM+QsAf"
REDIRECT_URI = "http://localhost:65010/monzo_callback"
def user_agent():
'''reddit API clients should each have their own, unique user-agent
Ideally, with contact info included.
e.g.,
return "oauth2-sample-app by /u/%s" % your_reddit_username
'''
raise NotImplementedError()
def base_headers():
return {"User-Agent": user_agent()}
app = Flask(__name__)
@app.route('/')
def homepage():
text = '<a href="%s">Authenticate with Monzo</a>'
return text % make_authorization_url()
def make_authorization_url():
# Generate a random string for the state parameter
# Save it for use later to prevent xsrf attacks
state = str(uuid4())
#save_created_state(state)
params = {"client_id": CLIENT_ID,
"response_type": "code",
"state": state,
"redirect_uri": REDIRECT_URI}
url = "https://auth.getmondo.co.uk/?" + urllib.parse.urlencode(params)
return url
# Left as an exercise to the reader.
# You may want to store valid states in a database or memcache.
def save_created_state(state):
pass
def is_valid_state(state):
return True
@app.route('/monzo_callback')
def monzo_callback():
error = request.args.get('error', '')
if error:
return "Error: " + error
state = request.args.get('state', '')
if not is_valid_state(state):
# Uh-oh, this request wasn't started by us!
abort(403)
code = request.args.get('code')
#access_token = get_token(code)
# Note: In most cases, you'll want to store the access token, in, say,
# a session for use in other parts of your web app.
return "Your reddit username is: %s" #% get_username(access_token)
#def get_token(code):
# client_auth = requests.auth.HTTPBasicAuth(CLIENT_ID, CLIENT_SECRET)
# post_data = {"grant_type": "authorization_code",
# "code": code,
# "redirect_uri": REDIRECT_URI}
# headers = base_headers()
# response = requests.post("https://ssl.reddit.com/api/v1/access_token",
# auth=client_auth,
# headers=headers,
# data=post_data)
# token_json = response.json()
# return token_json["access_token"]
#def get_username(access_token):
# headers = base_headers()
# headers.update({"Authorization": "bearer " + access_token})
# response = requests.get("https://oauth.reddit.com/api/v1/me", headers=headers)
# me_json = response.json()
# return me_json['name']
@app.route('/monzo_ExchangeAuthorizationCode')
def monzo_exchangeAuthorizationCode():
if __name__ == '__main__':
app.run(debug=False, port=65010)