|
| 1 | +import os |
| 2 | +from flask import Flask, render_template, request, url_for |
| 3 | +import oauth2 as oauth |
| 4 | +import urlparse |
| 5 | +import urllib |
| 6 | +import json |
| 7 | + |
| 8 | +app = Flask(__name__) |
| 9 | + |
| 10 | +app.debug = False |
| 11 | + |
| 12 | +request_token_url = 'https://twitter.com/oauth/request_token' |
| 13 | +access_token_url = 'https://twitter.com/oauth/access_token' |
| 14 | +authorize_url = 'https://twitter.com/oauth/authorize' |
| 15 | +show_user_url = 'https://api.twitter.com/1.1/users/show.json' |
| 16 | + |
| 17 | +# These are placeholders. You should add your keys to config.cfg |
| 18 | +app.config['APP_CONSUMER_KEY'] = 'API_Key_from_Twitter' |
| 19 | +app.config['APP_CONSUMER_SECRET'] = 'API_Secret_from_Twitter' |
| 20 | + |
| 21 | +# config.cfg should look like: |
| 22 | +# APP_CONSUMER_KEY = 'API_Key_from_Twitter' |
| 23 | +# APP_CONSUMER_SECRET = 'API_Secret_from_Twitter' |
| 24 | +app.config.from_pyfile('config.cfg', silent=True) |
| 25 | + |
| 26 | +oauth_store = {} |
| 27 | + |
| 28 | +@app.route('/') |
| 29 | +def hello(): |
| 30 | + return render_template('index.html') |
| 31 | + |
| 32 | +@app.route('/start') |
| 33 | +def start(): |
| 34 | + # Generate the OAuth request tokens, then display them |
| 35 | + app_callback_url = url_for('callback', _external=True) |
| 36 | + |
| 37 | + consumer = oauth.Consumer(app.config['APP_CONSUMER_KEY'], app.config['APP_CONSUMER_SECRET']) |
| 38 | + client = oauth.Client(consumer) |
| 39 | + resp, content = client.request(request_token_url, "POST", body=urllib.urlencode({"oauth_callback": app_callback_url})) |
| 40 | + if resp['status'] != '200': |
| 41 | + error_message = "Invalid response %s" % resp['status'] |
| 42 | + return render_template('error.html', error_message=error_message) |
| 43 | + |
| 44 | + request_token = dict(urlparse.parse_qsl(content)) |
| 45 | + oauth_token = request_token['oauth_token'] |
| 46 | + oauth_token_secret = request_token['oauth_token_secret'] |
| 47 | + |
| 48 | + oauth_store[oauth_token] = oauth_token_secret |
| 49 | + return render_template('start.html', authorize_url=authorize_url, oauth_token=oauth_token) |
| 50 | + |
| 51 | +@app.route('/callback') |
| 52 | +def callback(): |
| 53 | + # Accept the callback params, get the token and call the API to display this user's name and handle |
| 54 | + oauth_token = request.args.get('oauth_token') |
| 55 | + oauth_verifier = request.args.get('oauth_verifier') |
| 56 | + |
| 57 | + if not oauth_token or not oauth_verifier: |
| 58 | + return render_template('error.html', error_message="callback param(s) missing") |
| 59 | + |
| 60 | + # unless oauth_token is still stored locally, return error |
| 61 | + if oauth_token not in oauth_store: |
| 62 | + return render_template('error.html', error_message="oauth_token not found locally") |
| 63 | + |
| 64 | + oauth_token_secret = oauth_store[oauth_token] |
| 65 | + |
| 66 | + # if we got this far, we have both call back params and we have found this token locally |
| 67 | + |
| 68 | + consumer = oauth.Consumer(app.config['APP_CONSUMER_KEY'], app.config['APP_CONSUMER_SECRET']) |
| 69 | + token = oauth.Token(oauth_token, oauth_token_secret) |
| 70 | + token.set_verifier(oauth_verifier) |
| 71 | + client = oauth.Client(consumer, token) |
| 72 | + |
| 73 | + resp, content = client.request(access_token_url, "POST") |
| 74 | + access_token = dict(urlparse.parse_qsl(content)) |
| 75 | + |
| 76 | + screen_name = access_token['screen_name'] |
| 77 | + user_id = access_token['user_id'] |
| 78 | + |
| 79 | + # These are the tokens you would store long term, someplace safe |
| 80 | + real_oauth_token = access_token['oauth_token'] |
| 81 | + real_oauth_token_secret = access_token['oauth_token_secret'] |
| 82 | + |
| 83 | + # Call api.twitter.com/1.1/users/show.json?user_id={user_id} |
| 84 | + real_token = oauth.Token(real_oauth_token, real_oauth_token_secret) |
| 85 | + real_client = oauth.Client(consumer, real_token) |
| 86 | + real_resp, real_content = real_client.request(show_user_url + '?user_id=' + user_id, "GET") |
| 87 | + |
| 88 | + if real_resp['status'] != '200': |
| 89 | + error_message = "Invalid response from Twitter API GET users/show : %s" % real_resp['status'] |
| 90 | + return render_template('error.html', error_message=error_message) |
| 91 | + |
| 92 | + response = json.loads(real_content) |
| 93 | + |
| 94 | + friends_count = response['friends_count'] |
| 95 | + statuses_count = response['statuses_count'] |
| 96 | + followers_count = response['followers_count'] |
| 97 | + name = response['name'] |
| 98 | + |
| 99 | + # don't keep this token and secret in memory any longer |
| 100 | + del oauth_store[oauth_token] |
| 101 | + |
| 102 | + return render_template('callback-success.html', screen_name=screen_name, user_id=user_id, name=name, |
| 103 | + friends_count=friends_count, statuses_count=statuses_count, followers_count=followers_count) |
| 104 | + |
| 105 | +@app.errorhandler(500) |
| 106 | +def internal_server_error(e): |
| 107 | + return render_template('error.html', error_message='uncaught exception'), 500 |
| 108 | + |
| 109 | + |
0 commit comments