Skip to content

Instantly share code, notes, and snippets.

@seanbradley
Forked from oinume/config.py
Created March 8, 2012 12:54

Revisions

  1. @oinume oinume revised this gist Sep 11, 2011. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion root.py
    Original file line number Diff line number Diff line change
    @@ -43,7 +43,12 @@ def login():

    request_token = dict(parse_qsl(content))
    session['request_token'] = request_token
    url = 'https://api.twitter.com/oauth/authorize?oatuh_token=%s' % (request_token['oauth_token'])
    url = 'https://api.twitter.com/oauth/authorize?%s' % (
    urllib.urlencode({
    'oauth_token': request_token['oauth_token'],
    'oauth_callback': 'http://localhost:5000' + url_for('root.oauth_authorized')
    }).replace('+', '%20'),
    )
    return redirect(url)

    @app.route('/oauth_authorized')
  2. @oinume oinume revised this gist Sep 10, 2011. No changes.
  3. @oinume oinume revised this gist Sep 10, 2011. 2 changed files with 2 additions and 0 deletions.
    1 change: 1 addition & 0 deletions main.py
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    # app/main.py
    # -*- coding: utf-8 -*-
    from flask import Flask
    import config
    1 change: 1 addition & 0 deletions root.py
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    # app/views/root.py
    # -*- coding: utf-8 -*-
    from flask import (
    Blueprint,
  4. @oinume oinume created this gist Sep 10, 2011.
    34 changes: 34 additions & 0 deletions config.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    # app/config.py
    # -*- coding: utf-8 -*-
    import os

    class Config(object):
    DEBUG = False
    SQLALCHEMY_ECHO = False
    SECRET_KEY = 'dev_key_h8hfne89vm'
    CSRF_ENABLED = True
    CSRF_SESSION_LKEY = 'dev_key_h8asSNJ9s9=+'

    class DevelopmentConfig(Config):
    DEVELOPMENT = True
    DEBUG = True
    SQLALCHEMY_ECHO = True
    TWITTER_OAUTH_CONSUMER_KEY = 'your consumer key'
    TWITTER_OAUTH_CONSUMER_SECRET = 'your consumer secret'

    class TestingConfig(DevelopmentConfig):
    TESTING = True

    class ProductionConfig(Config):
    PRODUCTION = True

    mode = os.environ.get('TFAVFEED_ENV', 'development')
    object = DevelopmentConfig
    if mode == 'development':
    object = DevelopmentConfig
    elif mode == 'testing':
    object = TestingConfig
    elif mode == 'production':
    object = ProductionConfig
    else:
    raise ValueError("Unknown config mode.")
    14 changes: 14 additions & 0 deletions main.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    # -*- coding: utf-8 -*-
    from flask import Flask
    import config

    app = Flask(__name__)

    app.config.from_object(config.object)
    app.logger.info("config.object = %s" % config.object)

    from views import root
    app.register_blueprint(root.app)

    if __name__ == '__main__':
    app.run(debug = app.config['DEBUG'])
    72 changes: 72 additions & 0 deletions root.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    # -*- coding: utf-8 -*-
    from flask import (
    Blueprint,
    redirect,
    render_template,
    session,
    url_for,
    )
    import oauth2 as oauth
    import twitter
    import urllib

    import config

    # parse_qsl moved to urlparse module in v2.6
    try:
    from urlparse import parse_qsl
    except:
    from cgi import parse_qsl

    app = Blueprint('root', __name__)

    oauth_consumer = oauth.Consumer(
    key = config.object.TWITTER_OAUTH_CONSUMER_KEY,
    secret = config.object.TWITTER_OAUTH_CONSUMER_SECRET,
    )
    oauth_client = oauth.Client(oauth_consumer)

    @app.route('/login', methods = [ 'GET', 'POST' ])
    def login():
    res, content = oauth_client.request(
    'http://api.twitter.com/oauth/request_token?%s'
    % urllib.urlencode({
    'oauth_callback': 'http://localhost:5000' + url_for('root.oauth_authorized')
    }).replace('+', '%20'),
    'GET',
    )
    if res['status'] != '200':
    raise Exception(
    "Invalid response %s: %s" % (res['status'], content)
    )

    request_token = dict(parse_qsl(content))
    session['request_token'] = request_token
    url = 'https://api.twitter.com/oauth/authorize?oatuh_token=%s' % (request_token['oauth_token'])
    return redirect(url)

    @app.route('/oauth_authorized')
    def oauth_authorized():
    request_token = session['request_token']
    token = oauth.Token(
    request_token['oauth_token'],
    request_token['oauth_token_secret']
    )
    client = oauth.Client(oauth_consumer, token)
    res, content = client.request(
    'https://api.twitter.com/oauth/access_token',
    'POST'
    )
    if res['status'] != '200':
    raise Exception(
    "Invalid response %s: %s" % (res['status'], content)
    )
    access_token = dict(parse_qsl(content))

    api = twitter.Api(
    consumer_key = config.object.TWITTER_OAUTH_CONSUMER_KEY,
    consumer_secret = config.object.TWITTER_OAUTH_CONSUMER_SECRET,
    access_token_key = access_token['oauth_token'],
    access_token_secret = access_token['oauth_token_secret'],
    )
    return str(api.VerifyCredentials())