-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp-reddit.py
96 lines (85 loc) · 3.23 KB
/
app-reddit.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
96
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright 2014 J. Fernando Sánchez Rada - Grupo de Sistemas Inteligentes
# DIT, UPM
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
'''
Example flask application that uses the EUROSENTIMENT Sentiment Analysis
services to analyse posts from reddit.
'''
from flask import Flask, abort, request
from uuid import uuid4
import requests
import requests.auth
import urllib
import config
app = Flask(__name__)
app.debug = True
@app.route('/')
def homepage():
text = '<a href="%s">Authenticate with reddit</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": config.CLIENT_ID,
"response_type": "code",
"state": state,
"redirect_uri": config.REDIRECT_URI,
"duration": "temporary",
"scope": "identity"}
url = "https://ssl.reddit.com/api/v1/authorize?" + urllib.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('/reddit_callback')
def reddit_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)
return "Your reddit username is: %s" % get_username(access_token)
def get_token(code):
client_auth = requests.auth.HTTPBasicAuth(config.CLIENT_ID,
config.CLIENT_SECRET)
post_data = {"grant_type": "authorization_code",
"code": code,
"redirect_uri": config.REDIRECT_URI}
headers = {"User-agent": "/u/balkian's first reddit app"}
response = requests.post("https://ssl.reddit.com/api/v1/access_token",
auth=client_auth,
headers=headers,
data=post_data)
token_json = response.json()
print("Got token json")
print(token_json)
return token_json["access_token"]
def get_username(access_token):
headers = {"Authorization": "bearer " + access_token}
response = requests.get("https://oauth.reddit.com/api/v1/me", headers=headers)
me_json = response.json()
return me_json['name']
if __name__ == '__main__':
app.run(debug=True, port=5000)