|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# Copyright 2014 J. Fernando Sánchez Rada - Grupo de Sistemas Inteligentes |
| 4 | +# DIT, UPM |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +''' |
| 18 | +Example flask application that uses the EUROSENTIMENT Sentiment Analysis |
| 19 | +services to analyse posts from reddit. |
| 20 | +''' |
| 21 | +from flask import Flask, abort, request |
| 22 | +from uuid import uuid4 |
| 23 | +import requests |
| 24 | +import requests.auth |
| 25 | +import urllib |
| 26 | +import config |
| 27 | + |
| 28 | +app = Flask(__name__) |
| 29 | +app.debug = True |
| 30 | +@app.route('/') |
| 31 | +def homepage(): |
| 32 | + text = '<a href="%s">Authenticate with reddit</a>' |
| 33 | + return text % make_authorization_url() |
| 34 | + |
| 35 | + |
| 36 | +def make_authorization_url(): |
| 37 | + # Generate a random string for the state parameter |
| 38 | + # Save it for use later to prevent xsrf attacks |
| 39 | + state = str(uuid4()) |
| 40 | + save_created_state(state) |
| 41 | + params = {"client_id": config.CLIENT_ID, |
| 42 | + "response_type": "code", |
| 43 | + "state": state, |
| 44 | + "redirect_uri": config.REDIRECT_URI, |
| 45 | + "duration": "temporary", |
| 46 | + "scope": "identity"} |
| 47 | + url = "https://ssl.reddit.com/api/v1/authorize?" + urllib.urlencode(params) |
| 48 | + return url |
| 49 | + |
| 50 | + |
| 51 | +# Left as an exercise to the reader. |
| 52 | +# You may want to store valid states in a database or memcache. |
| 53 | +def save_created_state(state): |
| 54 | + pass |
| 55 | +def is_valid_state(state): |
| 56 | + return True |
| 57 | + |
| 58 | +@app.route('/reddit_callback') |
| 59 | +def reddit_callback(): |
| 60 | + error = request.args.get('error', '') |
| 61 | + if error: |
| 62 | + return "Error: " + error |
| 63 | + state = request.args.get('state', '') |
| 64 | + if not is_valid_state(state): |
| 65 | + # Uh-oh, this request wasn't started by us! |
| 66 | + abort(403) |
| 67 | + code = request.args.get('code') |
| 68 | + access_token = get_token(code) |
| 69 | + return "Your reddit username is: %s" % get_username(access_token) |
| 70 | + |
| 71 | +def get_token(code): |
| 72 | + client_auth = requests.auth.HTTPBasicAuth(config.CLIENT_ID, |
| 73 | + config.CLIENT_SECRET) |
| 74 | + post_data = {"grant_type": "authorization_code", |
| 75 | + "code": code, |
| 76 | + "redirect_uri": config.REDIRECT_URI} |
| 77 | + headers = {"User-agent": "/u/balkian's first reddit app"} |
| 78 | + response = requests.post("https://ssl.reddit.com/api/v1/access_token", |
| 79 | + auth=client_auth, |
| 80 | + headers=headers, |
| 81 | + data=post_data) |
| 82 | + token_json = response.json() |
| 83 | + print("Got token json") |
| 84 | + print(token_json) |
| 85 | + return token_json["access_token"] |
| 86 | + |
| 87 | + |
| 88 | +def get_username(access_token): |
| 89 | + headers = {"Authorization": "bearer " + access_token} |
| 90 | + response = requests.get("https://oauth.reddit.com/api/v1/me", headers=headers) |
| 91 | + me_json = response.json() |
| 92 | + return me_json['name'] |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == '__main__': |
| 96 | + app.run(debug=True, port=5000) |
0 commit comments