Skip to content

Instantly share code, notes, and snippets.

@Garciat
Created October 1, 2014 21:00
Show Gist options
  • Save Garciat/1c3b852c8422f6a03382 to your computer and use it in GitHub Desktop.
Save Garciat/1c3b852c8422f6a03382 to your computer and use it in GitHub Desktop.
requires pip packages: requests, requests_oauthlib
import requests
from requests_oauthlib import OAuth1Session
client_key = ''
client_secret = ''
request_token_url = 'https://api.twitter.com/oauth/request_token'
oauth = OAuth1Session(client_key, client_secret=client_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')
base_authorization_url = 'https://api.twitter.com/oauth/authorize'
authorization_url = oauth.authorization_url(base_authorization_url)
print('Please go here and authorize,', authorization_url)
verifier = input('Authorization PIN: ')
access_token_url = 'https://api.twitter.com/oauth/access_token'
oauth = OAuth1Session(client_key,
client_secret=client_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/user_timeline.json'
r = oauth.get(protected_url)
print(r.text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment