Last active
August 29, 2015 14:14
-
-
Save zzuf/eedb973402937d6855a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python | |
# -*- coding: utf-8 -*- | |
import urllib | |
import urllib2 | |
import cookielib | |
import oauth2 | |
import re | |
def PIN(url,SN,PW): | |
cj = cookielib.CookieJar() | |
#proxy_handler = urllib2.ProxyHandler({'http':'http://127.0.0.1:8080'}) | |
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) | |
headers = { | |
'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:35.0) Gecko/20100101 Firefox/30.0', | |
'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', | |
'Accept-Language' : 'ja,en-us;q=0.7,en;q=0.3', | |
'Connection' : 'close' | |
} | |
req = urllib2.Request(url,None,headers) | |
res = opener.open(req).read() | |
pt1 = r'<input name="authenticity_token" type="hidden" value="(.*)" />' | |
pt2 = r'<input id="oauth_token" name="oauth_token" type="hidden" value="(.*)" />' | |
authenticity_token = re.findall(pt1,res)[0] | |
oauth_token = re.findall(pt2,res)[0] | |
########################### | |
headers.update({'Referer' : url}) | |
params = urllib.urlencode({ | |
'authenticity_token' : authenticity_token, | |
'repost_after_login' : 'https://api.twitter.com/oauth/authorize', | |
'oauth_token' : oauth_token, | |
'session[username_or_email]' : SN, | |
'session[password]' : PW, | |
'remember_me' : '0', | |
}) | |
req = urllib2.Request('https://twitter.com/intent/sessions',params,headers) | |
url = opener.open(req).geturl() | |
########################### | |
headers.update({'Referer' : url}) | |
params = urllib.urlencode({ | |
'authenticity_token' : authenticity_token, | |
'oauth_token' : oauth_token, | |
}) | |
req = urllib2.Request('https://api.twitter.com/oauth/authorize',params,headers) | |
res = opener.open(req).read() | |
pt = r'<code>(.*)</code>' | |
pin = re.findall(pt,res)[0] | |
return pin | |
def OAuth_Key(CK,CS,SN,PW): | |
consumer = oauth2.Consumer(CK,CS) | |
client = oauth2.Client(consumer, None) | |
result = client.request('http://api.twitter.com/oauth/request_token','GET')[1] | |
result = dict([tuple(parameter.split('=')) for parameter in result.split('&')]) | |
token = oauth2.Token(result['oauth_token'],result['oauth_token_secret']) | |
tokenurl = "https://api.twitter.com/oauth/authenticate?oauth_token="+result['oauth_token'] | |
pin = PIN(tokenurl,SN,PW) | |
token.set_verifier(pin) | |
client.token = token | |
result = client.request('http://api.twitter.com/oauth/access_token','POST')[1] | |
token = dict([tuple(parameter.split('=')) for parameter in result.split('&')]) | |
return token | |
if __name__ == '__main__': | |
#取得済みのCKとCS | |
CK = raw_input('CONSUMER_KEY = ') | |
CS = raw_input('CONSUMER_SECRET = ') | |
#TwitterID | |
SN = raw_input('TwitterID = ') | |
#TwitterPassword | |
PW = raw_input('TwitterPassword = ') | |
token = OAuth_Key(CK,CS,SN,PW) | |
AT = token['oauth_token'] | |
AS = token['oauth_token_secret'] | |
print 'CONSUMER_KEY: %s' % CK | |
print 'CONSUMER_SECRET: %s' % CS | |
print 'ACCESS_TOKEN_KEY: %s' % AT | |
print 'ACCESS_TOKEN_SECRET: %s' % AS | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment