"Because fighting with API changes shouldn't be a full-time job"
Last Updated: [2025-03-18]
Tested With: Tweepy 4.X | Python 3.X | Twitter API v2
Twitter's developer ecosystem underwent a seismic shift in 2023, leaving most pre-existing tutorials outdated and useless. This guide cuts through the noise, delivering a working OAuth 2.0 authentication process using Python and Tweepy. Let's get you connected to the Twitter API—fast.
Before diving in, ensure you have everything set up:
- Developer Account: You need one. If you don't have it, apply at developer.twitter.com.
- OAuth 2.0 Enabled: In your project's settings, confirm OAuth 2.0 is activated.
- Callback URI: Set to https://localhost (exactly as written—no trailing slashes).
- Scopes: Add tweet.read, tweet.write, and users.read in the User Authentication Settings.
- Python 3.6+: Check your version with python --version.
- Tweepy 4.0+: Install or update via pip install tweepy --upgrade.
- Optional: A coffee, tea, or energy drink to power through this.
Here's how to authenticate with the Twitter API using OAuth 2.0, broken down into clear, actionable steps.
Start by importing Tweepy and defining your credentials. You'll find your client_id and client_secret in the Twitter Developer Portal under your project's settings.
import tweepy
# Replace these with your actual credentials
client_id = "YOUR_CLIENT_ID"
client_secret = "YOUR_CLIENT_SECRET"
redirect_uri = "https://localhost"
Tip: Store credentials securely (e.g., in environment variables) and never commit them to version control.
Use Tweepy's OAuth2UserHandler to kick off the authentication process. Pass in your credentials and the required scopes.
oauth2_user_handler = tweepy.OAuth2UserHandler(
client_id=client_id,
redirect_uri=redirect_uri,
scope=["tweet.read", "tweet.write", "users.read"],
client_secret=client_secret
)
Generate the URL you'll use to authorize your app, then visit it in your browser.
auth_url = oauth2_user_handler.get_authorization_url()
print(f"🔥 Visit this URL to authorize: {auth_url}")
Action: Open the printed URL in your browser while logged into the Twitter account you want to authenticate. Authorize the app when prompted.
After authorizing, Twitter redirects you to a URL like this:
https://localhost/?state=XXXX&code=YYYY
Action: Copy the entire URL from your browser's address bar. This contains the authorization code you need.
Back in your Python script, paste the callback URL when prompted to retrieve your access token.
response_url = input("Paste the full callback URL here: ").strip()
access_token = oauth2_user_handler.fetch_token(response_url)
With the access token in hand, create a Client object to interact with the Twitter API.
client = tweepy.Client(access_token["access_token"])
Verify everything works by posting a tweet (or another action based on your scopes).
response = client.create_tweet(
text="I survived Twitter's OAuth apocalypse! 🚀",
user_auth=False # Important: Keep this as False
)
print(f"🚀 Tweet posted: https://twitter.com/user/status/{response.data['id']}")
If this works, congratulations—you're authenticated!
Don't let these trip you up:
Fix: Ensure https://localhost matches exactly in both your code and the Developer Portal. No trailing slashes (e.g., https://localhost/ is wrong).
Fix: In the Developer Portal, go to User Authentication Settings, add the scopes (tweet.read, tweet.write, users.read), and regenerate your credentials if needed.
Fix: Always use user_auth=False with Client. The access token already includes user context, so extra authentication isn't required. Avoid the outdated tweepy.API()—stick to Client().
- Token Lifetime: OAuth 2.0 tokens don't expire until manually revoked.
- Production Tip: Replace https://localhost with your real callback URL when deploying.
- Security: Use environment variables (e.g., os.environ["CLIENT_ID"]) to keep credentials safe.
Before celebrating, double-check:
- OAuth 2.0 (not 1.0a) is enabled in the Developer Portal.
- Callback URI is https://localhost (no variations).
- You copied the full callback URL, including ?state=...&code=....
- user_auth=False is set in Client method calls.
You've conquered Twitter's OAuth 2.0 authentication maze. Whether you're building bots, analyzing trends, or automating tweets, the API is now yours to command. Go forth and create!
"First they ignore you, then they change the API, then you write a README about it." – Not Gandhi