Skip to content

Instantly share code, notes, and snippets.

@anapaulagomes
Created October 11, 2018 13:54
Show Gist options
  • Save anapaulagomes/fcc71dad7e7e4ec2809992341f96cab2 to your computer and use it in GitHub Desktop.
Save anapaulagomes/fcc71dad7e7e4ec2809992341f96cab2 to your computer and use it in GitHub Desktop.
Twitter OAuth1
import os
from requests_oauthlib import OAuth1Session
request_token_url = 'https://api.twitter.com/oauth/request_token'
base_authorization_url = 'https://api.twitter.com/oauth/authorize'
access_token_url = 'https://api.twitter.com/oauth/access_token'
oauth = OAuth1Session(os.getenv('CONSUMER_KEY'), client_secret=os.getenv('CONSUMER_SECRET'))
fetch_response = oauth.fetch_request_token(request_token_url)
resource_owner_key = fetch_response.get('oauth_token')
resource_owner_secret = fetch_response.get('oauth_token_secret')
authorization_url = oauth.authorization_url(base_authorization_url)
print('Please go here and authorize', authorization_url)
redirect_response = input('Paste the full redirect URL here: ')
oauth_response = oauth.parse_authorization_response(redirect_response)
verifier = oauth_response.get('oauth_verifier')
oauth = OAuth1Session(os.getenv('CONSUMER_KEY'),
client_secret=os.getenv('CONSUMER_SECRET'),
resource_owner_key=resource_owner_key,
resource_owner_secret=resource_owner_secret,
verifier=verifier)
oauth_tokens = oauth.fetch_access_token(access_token_url)
resource_owner_key = oauth_tokens.get('oauth_token')
resource_owner_secret = oauth_tokens.get('oauth_token_secret')
protected_url = 'https://api.twitter.com/1.1/statuses/home_timeline.json'
response = oauth.get(protected_url)
print(response)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment