Created
February 12, 2011 13:45
-
-
Save dnpp73/823770 to your computer and use it in GitHub Desktop.
appの認証して得たtokenをyamlに書き出すだけ。
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/bin/python | |
#-*- encoding:utf-8 -*- | |
ckey = "" | |
csecret = "" | |
import time, random | |
import urllib, urllib2 | |
import hmac, hashlib | |
import cgi | |
import hashlib | |
import yaml | |
from time import sleep , time ,strftime,localtime | |
import os | |
cdir = os.path.abspath(os.path.dirname(__file__)) | |
oauth_yaml_path = os.path.normpath(os.path.join(cdir,"../oauth_yaml","token.yaml")) | |
def make_signature(params, url, method, csecret, secret = ""): | |
# Generate Signature Base String | |
plist = [] | |
for i in sorted(params): | |
plist.append("%s=%s" % (i, params[i])) | |
pstr = "&".join(plist) | |
msg = "%s&%s&%s" % (method, urllib.quote(url, ""),urllib.quote(pstr, "")) | |
# Calculate Signature | |
h = hmac.new("%s&%s" % (csecret, secret), msg, hashlib.sha1) | |
sig = h.digest().encode("base64").strip() | |
return sig | |
def init_params(): | |
p = { | |
"oauth_consumer_key": ckey, | |
"oauth_signature_method": "HMAC-SHA1", | |
"oauth_timestamp": str(int(time())), | |
"oauth_nonce": str(random.getrandbits(64)), | |
"oauth_version": "1.0" | |
} | |
return p | |
reqt_url = 'http://twitter.com/oauth/request_token' | |
auth_url = 'http://twitter.com/oauth/authorize' | |
acct_url = 'http://twitter.com/oauth/access_token' | |
def get_oauth_token(): | |
try: | |
# Request Parameters | |
params = init_params() | |
print "---* Get request token:", | |
# Generate Signature | |
sig = make_signature(params, reqt_url, "GET", csecret) | |
params["oauth_signature"] = sig | |
# Get Token | |
req = urllib2.Request("%s?%s" % (reqt_url, urllib.urlencode(params))) | |
resp = urllib2.urlopen(req) | |
except (urllib2.HTTPError,urllib2.URLError): | |
print "\t[failed]" | |
print "---! confirm ConsumerKey and ConsumerSecret OR maybe app is Inactive" | |
quit() | |
else: | |
print "\t[OK]" | |
try: | |
# Parse Token Parameters | |
ret = cgi.parse_qs(resp.read()) | |
token = ret["oauth_token"][0] | |
token_secret = ret["oauth_token_secret"][0] | |
# Get PIN | |
print "---* Please access to this URL, and allow." | |
print "[ %s?%s=%s ]" % (auth_url, "oauth_token", token) | |
print "---* After that, will display 7 digit PIN, input here." | |
print "->", | |
pin = raw_input() | |
pin = int(pin) | |
print "---* Get access token", | |
# Generate Access Token Request | |
params = init_params() | |
params["oauth_verifier"] = pin | |
params["oauth_token"] = token | |
sig = make_signature(params, acct_url, "GET", csecret, token_secret) | |
params["oauth_signature"] = sig | |
# Get Access Token | |
req = urllib2.Request("%s?%s" % (acct_url, urllib.urlencode(params))) | |
resp = urllib2.urlopen(req) | |
except (urllib2.HTTPError,urllib2.URLError): | |
print "\t[failed]" | |
print "---! Please retry" | |
quit() | |
else: | |
print "\t[ OK ]" | |
# Parse Access Token | |
fin = cgi.parse_qs(resp.read()) | |
atoken = fin["oauth_token"][0] | |
atoken_secret = fin["oauth_token_secret"][0] | |
oauth_dict = {"consumer_key":ckey, "consumer_secret":csecret, "oauth_token":atoken, "oauth_token_secret":atoken_secret} | |
if not os.path.exists(os.path.dirname(oauth_yaml_path)): | |
os.mkdir(os.path.dirname(oauth_yaml_path)) | |
f = open(oauth_yaml_path,"w") | |
yaml.dump(oauth_dict,f,default_flow_style=False) | |
f.close() | |
print "---> Done" | |
if __name__ == "__main__": | |
try: | |
get_oauth_token() | |
except KeyboardInterrupt: | |
print "\n---> see you !" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment