Skip to content

Instantly share code, notes, and snippets.

@FiNGAHOLiC
Created January 24, 2012 09:52
Show Gist options
  • Select an option

  • Save FiNGAHOLiC/1669332 to your computer and use it in GitHub Desktop.

Select an option

Save FiNGAHOLiC/1669332 to your computer and use it in GitHub Desktop.
Twitter OAuth by Python
#! /usr/bin/env python
# coding: utf-8
# http://blog.hogeo.jp/2011/05/how-to-make-twitter-bot-python-twitter.html
from oauth2 import Client, Token, Consumer
consumer_key = 'ENTER_CONSUMER_KEY'
consumer_secret = 'ENTER_CUNSUMER_SECRET'
def split_parameter(parameters):
result_list = [tuple(parameter.split('=')) for parameter in parameters.split('&')]
return dict(result_list)
consumer = Consumer(consumer_key, consumer_secret)
client = Client(consumer, None)
result = client.request('http://api.twitter.com/oauth/request_token', 'GET')
request_token_map = split_parameter(result[1])
request_token = Token(request_token_map['oauth_token'], request_token_map['oauth_token_secret'])
print 'Please access "http://api.twitter.com/oauth/authorize?oauth_token=' + request_token.key + '".'
pin = raw_input('PIN:')
request_token.set_verifier(pin)
client.token = request_token
result = client.request('http://api.twitter.comoauth/access_token', 'POST')
access_token_map = split_parameter(result[1])
print result[1]
print 'USER KEY : ' + access_token_map['oauth_token']
print 'USER SECRET : ' + access_token_map['oauth_token_secret']
raw_input('Push any key to quit.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment