Skip to content

Instantly share code, notes, and snippets.

@yarlson
Created September 25, 2023 18:52
Show Gist options
  • Save yarlson/b383b582e74c89b84e8098c5da85211a to your computer and use it in GitHub Desktop.
Save yarlson/b383b582e74c89b84e8098c5da85211a to your computer and use it in GitHub Desktop.
import os
from flask import Flask, request, redirect
from requests_oauthlib import OAuth1Session
import webbrowser
app = Flask(__name__)
# Your API Key and API Secret Key from the Twitter Developer Dashboard
API_KEY = os.environ.get('CONSUMER_KEY')
API_SECRET_KEY = os.environ.get('CONSUMER_SECRET')
# Twitter endpoints
REQUEST_TOKEN_URL = 'https://api.twitter.com/oauth/request_token'
AUTHORIZATION_URL = 'https://api.twitter.com/oauth/authorize'
ACCESS_TOKEN_URL = 'https://api.twitter.com/oauth/access_token'
oauth_callback = 'http://localhost:8080/callback'
initial_oauth = OAuth1Session(API_KEY, client_secret=API_SECRET_KEY, callback_uri=oauth_callback)
@app.route('/')
def start():
oauth = OAuth1Session(API_KEY, client_secret=API_SECRET_KEY, callback_uri=oauth_callback)
authorization_url = oauth.authorization_url(AUTHORIZATION_URL)
return redirect(authorization_url)
@app.route('/callback')
def callback():
oauth_response = initial_oauth.parse_authorization_response(request.url)
oauth_token = oauth_response.get('oauth_token')
oauth_verifier = oauth_response.get('oauth_verifier')
oauth = OAuth1Session(
API_KEY,
client_secret=API_SECRET_KEY,
resource_owner_key=oauth_token,
verifier=oauth_verifier
)
tokens = oauth.fetch_access_token(ACCESS_TOKEN_URL)
return tokens
if __name__ == "__main__":
webbrowser.open("http://localhost:8080/")
app.run(port=8080)
@yarlson
Copy link
Author

yarlson commented Sep 25, 2023

Twitter OAuth1 Authentication with Flask

This script sets up a simple Flask server that helps you authenticate with Twitter using the OAuth1 protocol and obtain the access token and token secret.

Prerequisites

  1. Python: The script is written in Python. Ensure you have Python installed on your machine.
  2. Twitter Developer Account: If you don’t already have a Twitter developer account, you'll need to create one. Visit the Twitter Developer portal to create an account.

Installation

Install the Dependencies

Install the necessary Python packages using pip:

pip install Flask requests_oauthlib webbrowser

Configuration

Set up Twitter Consumer Keys

  1. Once you have a Twitter developer account, create a new App from the Developer Dashboard.
  2. After creating the app, navigate to the Keys and Tokens tab, thenConsumer Keys, Generate

Environment Variables

Before running the script, you need to set up a few environment variables. You can do this manually or use a .env file.

For manual setup:

export CONSUMER_KEY='YOUR_API_KEY'
export CONSUMER_SECRET='YOUR_API_SECRET_KEY'

Running the Script

Once you have set up the necessary configurations, you can run the script:

python [YOUR SCRIPT NAME].py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment