Skip to content

Instantly share code, notes, and snippets.

@anapaulagomes
Created October 7, 2018 20:28
Show Gist options
  • Save anapaulagomes/7056562dc98254f8e9efc50d40bf7eea to your computer and use it in GitHub Desktop.
Save anapaulagomes/7056562dc98254f8e9efc50d40bf7eea to your computer and use it in GitHub Desktop.
Twython + Flask
from flask import Flask, url_for, session, request, redirect
import os
from cube.app import app, AuthToken
from cube.api import get_twitter_api
import twython
import time
api = get_twitter_api()
@app.route('/')
def index():
return 'It works!'
@app.route('/authorize')
def oauth_authorize():
twitter = twython.Twython(os.getenv('CONSUMER_KEY', 'xxx'), os.getenv('CONSUMER_SECRET', 'yyy'))
auth = twitter.get_authentication_tokens(callback_url='http://localhost:5000/callback')
session['request_token'] = [auth['oauth_token'], auth['oauth_token_secret']]
return redirect(auth['auth_url'])
@app.route('/callback')
def oauth_callback():
oauth_verifier = request.args.get('oauth_verifier')
request_token = session.pop('request_token')
twitter = twython.Twython(
os.getenv('CONSUMER_KEY', 'xxx'),
os.getenv('CONSUMER_SECRET', 'yyy'),
request_token[0],
request_token[1]
)
final_step = twitter.get_authorized_tokens(oauth_verifier)
# TODO save on db
OAUTH_TOKEN = final_step['oauth_token']
OAUTH_TOKEN_SECRET = final_step['oauth_token_secret']
return f'Welcome, {screen_name}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment