ãã夫㨠Python ã§å¦ã¶ Twitter ã® OAuth
OAuth 調ã¹ã¦ã¿ããé£ããã¦ç解åºæ¥ãªãã£ãã®ã§ï¼
Python ã§æ¨æºã©ã¤ãã©ãªã ã㧠1 ããæ¸ãã¦ã¿ã¾ããï¼
ã ãããï¼ãã ã ãï¼¼ ãããï¼ã _ã ãã½ï½¤_ã ï¼¼ ã ï¼ oï¾(ï¼âï¼) (ï¼âï¼)ï¾o ï¼¼ ãtwitter ã® OAuth é£ããã⦠ã |ãããã ï¼__人__ï¼'ãããã| ã ï¼¼ãã ããï½â´ ã ã ï¼ ã ã ããã____ ã ãããï¼âããâï¼¼ ãããï¼ï¼ âï¼ ãï¼âï¼ï¼¼ ã ï¼::::::âï¼__人__ï¼â::::: ï¼¼ ããã ãããã夫ã§ãããï¼ ã |ããããã|râ¬-|ããããã| ã ï¼¼ ãã ã `ã¼'´ ã ã ï¼
Python ã®ãµã³ãã«ã³ã¼ããä»ãã¦ãã¾ããï¼
ä¸ããé ã«èªããããã«ãããã£ããæç¶ãåã§æ¸ãã¦ãã¾ãï¼
ã³ã¡ã³ãã§å®éã®å¦çã®èª¬æãæ¸ãã¦ãã¾ãï¼
Consumer Key 㨠Consumer Secret ã®å ¥æ
ããããããã ï¼ï¿£ï¿£ï¼¼ããã ããããããï¼ããã uã ï¼¼ ããããã.____ ããã ãããããã|:::::: ã ãã uãï½ããã.ï¼ãã ã ãï¼¼ ãOAuth åºæ¥ãããã«ãããã®ã§ ããããã.ã|:::::::::::ãããããï½ã ï¼ âãããâã ï¼¼ãã¢ã¯ã»ã¹ãã¼ã¯ã³ãã ãã ãããããã |:::::::::::u:ããããï½ï¼ ãï¼âï¼ãï¼âï¼ ããï¼¼ ããããã.ã |::::::::::::::ããuã } |ã ããï¼__人__ï¼ããããã|ã ããããã.ã ã½:::::::::::::: ãã }ãï¼¼ãã ï½ â´ ãããã_ï¼ã ããããããã ã½::::::::::ãããããã|ãããããããããã ï¼¼ ãããããããã/:::::::::::: ããããã|ã|ãããããããã | ã| -âââââ|:::::::::::::::: ï¼¼-ââ´â´ââââââ´â´ââãã ãããããã ã |:::::::::::::::|ã½ãäºâ) ãããããããï¼ãã®ç»é¢ã§ç»é²ãã¦ãã ããã ã ããã ãã ã____ ãããããã ï¼ ï¼¼ããï¼ï¼¼ãï½·ï¾ï½¯ .ãããããï¼ãï¼ã¼ï¼ ãï¼ã¼ï¼ï¼¼ãããããããªã®ç°¡åã ã ããããï¼ãã âï¼__人__ï¼â ï¼¼ ãããã|ãã ããã|râ¬-|ãããã| ãããã ï¼¼ãããã `ã¼'´ãã ï¼ ããããããããããããããã ãï¼¼ ã ï¼Â´ãããããããããããã ããã½ ã|ããããï½ããããããããããããããï¼¼ ãã½ããã -ä¸''''''"~~ï½`'ã¼--、ããã-ä¸'''''''ã¼-、. ãã㽠____(â)(â)â)ã)ãã(â_(â)â)â)) ãããããããããã ___ãããââ ãããããããããï¼ãâããï¼¼ã ââ ãããããã ãï¼ãã(â)ã \㽠㻠ãããã ãã/ãã ï¼âãã(â) /ãããï¼ï¼ ããã«ç»é²ãã¦ã Consumer Key 㨠Consumer Secret ãã ããã ãã /ãã ã ï¿£ã½__ï¼ ï¼ãæã«ã¯ãããªããï¼ .ããããï¼Â´ããããã___ï¼ã ãããã|ããããã ã ï¼¼ ãããã|ãããããããã|
ã¨ããããã§ï¼
http://dev.twitter.com/apps/new
ã«ã¢ã¯ã»ã¹ãã¦ç»é²ãã㨠Consumer Key 㨠Consumer Secret ãæã«å
¥ãã¾ãï¼
ãªã¯ã¨ã¹ããã¼ã¯ã³ã®åå¾
ããããããã ï¼ï¿£ï¿£ï¼¼ããã ããããããï¼ããã uã ï¼¼ ããããã.____ ããã ãããããã|:::::: ã ãã uãï½ããã.ï¼ãã ã ãï¼¼ ãConsumer Key 㨠Consumer Secret ãæã«å ¥ããã®ã§ ããããã.ã|:::::::::::ãããããï½ã ï¼ âãããâã ï¼¼ãã¢ã¯ã»ã¹ãã¼ã¯ã³ãã ãã ãããããã |:::::::::::u:ããããï½ï¼ ãï¼âï¼ãï¼âï¼ ããï¼¼ ããããã.ã |::::::::::::::ããuã } |ã ããï¼__人__ï¼ããããã|ã ããããã.ã ã½:::::::::::::: ãã }ãï¼¼ãã ï½ â´ ãããã_ï¼ã ããããããã ã½::::::::::ãããããã|ãããããããããã ï¼¼ ãããããããã/:::::::::::: ããããã|ã|ãããããããã | ã| -âââââ|:::::::::::::::: ï¼¼-ââ´â´ââââââ´â´ââãã ãããããã ã |:::::::::::::::|ã½ãäºâ) ããããã¯ï¼å¥ã®çªå£(URL)ã§ç³è«ãã¦ä¸ãã ãã ãããï¼ï¿£ï¿£ï¿£ï¼¼ ããããï¼ãâã ã â ï¼¼ãæ¥æ¬ã®å½¹æãã!ï¼ ãã ï¼ã ï¼âï¼ ãï¼âï¼ã ï¼¼ã ãã |ããããï¼__人__ï¼ãããã| ãã ï¼¼ããã ï½ â´.ãã ãï¼ã ãã ï¼ ã ã ãããã ã ã ï¼¼ ã ã ãããï¼ï¿£ï¿£ï¿£ ï¼¼ ãï¾ï½¼ï¾ï¾ï½¼ï¾ ã ãããï¼ãâãããâãï¼¼ ã ã ï¼ ã ï¼âï¼ ãï¼âï¼ããï¼¼ãã¢ã¯ã»ã¹ãã¼ã¯ã³ä¸ãã ã ã |ã ãããï¼__人__ï¼ ãã ã | ã ã ï¼¼ãã ï½j |â´ ãã ãï¼ ã ãããã ã__ï¾ ã ããããã ã ï¾ ãã ï¼ï¿£ï¿£ï¼¼ ãï¼ãã _ãããï¼¼ã ã|ããã ï¼ âï¼ï¼âï¼ãã¢ã¯ã»ã¹ãã¼ã¯ã³ã®åå¾ã«ã¯ãªã¯ã¨ã¹ããã¼ã¯ã³ãå¿ è¦ã§ãã®ã§ .ã|ãããã ï¼__人__ï¼ããã¡ãã®å½¢å¼ã§èªè¨¼ãã¦ãã ãã ã |ãããããï½ â´ï¾ãã .ã |ãããããã ã } .ã ã½ããããã ã } ãã ã½ããããããããããããããï¼¼ ããã/ããã ãããï¼¼ããããããã ï¼¼ ããã|ãããã ï¼¼ ã ï¼¼ ã ããããããï¼¼ ã ã |ãããã|ã½ãäºâ)、ã ã ããããã ï¼¼ ã ã ã ã ã ã___ ãããããããï¼ãã ã ãï¼¼ ãããã ãï¼ãããï¼¼ãã u. ï¼¼ã(ã¡ãããªãã ãé¢åèããªã£ã¦ãã¦ãã) ããããï¼ ï¼âï¼ ãï¼âï¼ ãããï¼¼ã ãã ã |ã ãï¼__人__ï¼ã ããu. ã | ã ãã ï¼¼ãu.ï½ â´ ã ããããï¼ ããããããããããã ãããããï¼¼ ã ï¼Â´ãããããããããããã ããã½
ã¨ããããã§ï¼ãªã¯ã¨ã¹ããã¼ã¯ã³ã®åå¾ã§ãï¼
Python ã§æ¸ãã¦ã¿ã¾ã
#!/usr/bin/env python # -*- coding: utf-8 -*- import time, random, urllib, cgi, hmac, hashlib consumer_key = 'ãã£ãã®ç»é¢ã«åºã¦ãããã¤' consumer_secret = 'ãã£ãã®ç»é¢ã«åºã¦ãããã¤' # Request Token 㯠GET ã§åå¾ method = 'GET' # Request Token åå¾ã® URL request_token_url = 'http://twitter.com/oauth/request_token' # å¿ é ãã©ã¡ã¼ã¿ãæºå params = { "oauth_consumer_key": consumer_key, # WEB ã§ç»é²ãã oauth consumer key "oauth_signature_method": "HMAC-SHA1", # æå·ã®ã¢ã«ã´ãªãºã "oauth_timestamp": str(int(time.time())), # unixtime "oauth_nonce": str(random.getrandbits(64)), # ã©ã³ãã æåå "oauth_version": "1.0" # ãã¼ã¸ã§ã³çªå· } # ãã©ã¡ã¼ã¿ãã½ã¼ããï¼URIã¨ã³ã³ã¼ããã key=value ã®å½¢ã«ã㦠& ã§ç¹ãã¾ã params_str = '&'.join(['%s=%s' % (urllib.quote(key, ''),urllib.quote(params[key], '')) for key in sorted(params)]) # ã¡ã½ããï¼URIã¨ã³ã³ã¼ããã URL,ä¸ã§ä½ã£ããã©ã¡ã¼ã¿æååã & ã§ç¹ãã¾ã message = '%s&%s&%s' % (method,urllib.quote(request_token_url,''), urllib.quote(params_str,'')) # consumer_secret ãå ã«ãã¼ãä½æãã¾ã key = "%s&%s" % (consumer_secret, '') # ãã¼ãå ã« message 㧠hmac ãä½æãã¾ã signature = hmac.new(key, message, hashlib.sha1) # ãã®ãã¤ã¸ã§ã¹ãã base64 ã§ã¨ã³ã³ã¼ããï¼ä½è¨ãªç©ºç½ãåãé¤ãã¾ã digest_base64 = signature.digest().encode("base64").strip() # ä½æãããã¤ã¸ã§ã¹ãããã©ã¡ã¼ã¿ã«è¿½å ãã¾ã params['oauth_signature'] = digest_base64 # ä½æãããã©ã¡ã¼ã¿ã GET ã®ãã©ã¡ã¼ã¿ã¨ãã¦è¿½å ãã¾ã _url = request_token_url+ '?' + urllib.urlencode(params) # ãã® URL ã«ã¢ã¯ã»ã¹ã, ResponseBody ãèªã¿è¾¼ã¿ã¾ã result = urllib.urlopen(_url).read() # ãã¼ã¹ãã¾ã data = cgi.parse_qs(result) # åå¾ã§ãã¾ãã!! print data #{'oauth_token_secret': ['xxx'], 'oauth_token': ['xxxx'], 'oauth_callback_confirmed': ['true']} # oauth_token ããªã¯ã¨ã¹ããã¼ã¯ã³
注æç¹ã¨ãã¦ã¯ï¼ããã§åå¾åºæ¥ã oauth_token,oauth_token_secret ã¯ä¸æçãªãã®ã ã¨ããäºã§ãï¼
ãã®å¾ã«åºã¦ããã¢ã¯ã»ã¹ãã¼ã¯ã³ã®åå¾ãè¡ãªã£ãå¾ã¯ç¡å¹ã«ãªãã¾ãã®ã§æ³¨æãã¦ãã ããï¼
OAuth Verifier ã®åå¾
ããããããã ï¼ï¿£ï¿£ï¼¼ããã ããããããï¼ããã uã ï¼¼ ããããã.____ ããã ãããããã|:::::: ã ãã uãï½ããã.ï¼ãã ã ãï¼¼ ãOAuth Token 㨠OAuth Token Secret ãæã«å ¥ããã®ã§ ããããã.ã|:::::::::::ãããããï½ã ï¼ âãããâã ï¼¼ãã¢ã¯ã»ã¹ãã¼ã¯ã³ãã ãã ãããããã |:::::::::::u:ããããï½ï¼ ãï¼âï¼ãï¼âï¼ ããï¼¼ ããããã.ã |::::::::::::::ããuã } |ã ããï¼__人__ï¼ããããã|ã ããããã.ã ã½:::::::::::::: ãã }ãï¼¼ãã ï½ â´ ãããã_ï¼ã ããããããã ã½::::::::::ãããããã|ãããããããããã ï¼¼ ãããããããã/:::::::::::: ããããã|ã|ãããããããã | ã| -âââââ|:::::::::::::::: ï¼¼-ââ´â´ââââââ´â´ââãã ãããããã ã |:::::::::::::::|ã½ãäºâ) ãããã¡ãã¯ãªã¯ã¨ã¹ããã¼ã¯ã³ã®çªå£(URL)ã§ãã®ã§ï¼ããã¯å¥ã®çªå£(URL)ã§ç³è«ãã¦ä¸ãã ãã ãããï¼ï¿£ï¿£ï¿£ï¼¼ ããããï¼ãâã ã â ï¼¼ãã¾ããã!ï¼ ãã ï¼ã ï¼âï¼ ãï¼âï¼ã ï¼¼ã ãã |ããããï¼__人__ï¼ãããã| ãã ï¼¼ããã ï½ â´.ãã ãï¼ã ãã ï¼ ã ã ãããã ã ã ï¼¼ ã ã ãããï¼ï¿£ï¿£ï¿£ ï¼¼ ãï¾ï½¼ï¾ï¾ï½¼ï¾ ã ãããï¼ãâãããâãï¼¼ ã ã ï¼ ã ï¼âï¼ ãï¼âï¼ããï¼¼ãã¢ã¯ã»ã¹ãã¼ã¯ã³ä¸ãã ã ã |ã ãããï¼__人__ï¼ ãã ã | ã ã ï¼¼ãã ï½j |â´ ãã ãï¼ ã ãããã ã__ï¾ ã ããããã ã ï¾ ãã ï¼ï¿£ï¿£ï¼¼ ãï¼ãã _ãããï¼¼ã ã|ããã ï¼ âï¼ï¼âï¼ãã¢ã¯ã»ã¹ãã¼ã¯ã³ã®åå¾ã«ã¯ã¦ã¼ã¶ã«èªè¨¼ãã¦ããã£ã¦ .ã|ãããã ï¼__人__ï¼ãOAuth Verifier ãåå¾ãã¦ãããå¿ è¦ãããã¾ã ã |ãããããï½ â´ï¾ãã .ã |ãããããã ã } .ã ã½ããããã ã } ãã ã½ããããããããããããããï¼¼ ããã/ããã ãããï¼¼ããããããã ï¼¼ ããã|ãããã ï¼¼ ã ï¼¼ ã ããããããï¼¼ ã ã |ãããã|ã½ãäºâ)、ã ã ããããã ï¼¼ ã ã ã ã ã ã___ ãããããããï¼ãã ã ãï¼¼ ãããã ãï¼ãããï¼¼ãã u. ï¼¼ã(ã¡ããã¾ã è²°ããªãã®ãã) ããããï¼ ï¼âï¼ ãï¼âï¼ ãããï¼¼ã ãã ã |ã ãï¼__人__ï¼ã ããu. ã | ã ãã ï¼¼ãu.ï½ â´ ã ããããï¼ ããããããããããã ãããããï¼¼ ã ï¼Â´ãããããããããããã ããã½
ã¨ããããã§ï¼OAuth Verifier ã®åå¾ã§ããï¼ä»åã¯ã¯ã©ã¤ã¢ã³ãã¨ãã¦ä½¿ãããã®ã§ï¼
åç´ã«å
ç¨åå¾ãã oauth_token ããã©ã¡ã¼ã¿ã«ä»ãã¦ã¢ã¯ã»ã¹ããã ãã§ãï¼
# ãã£ãåå¾ãããã¼ã¿ãã oauth_token ãåå¾ãã¦ã²ã£ã¤ãã print 'http://twitter.com/oauth/authorize?oauth_token='+data['oauth_token'][0]
ããã§è¡¨ç¤ºãããã¢ãã¬ã¹ã«ãã©ã¦ã¶ã§ã¢ã¯ã»ã¹ãï¼ã¢ããªã±ã¼ã·ã§ã³ããã®ä½¿ç¨ã許å¯ããã¨
ç»é¢ã« OAuth Verifier ã表示ããã¾ãï¼
ã¢ã¯ã»ã¹ãã¼ã¯ã³ã®åå¾
ãããããããã¹ï¼å¥ã®çªå£(URL)ã«ãªãã¾ã⦠ããããããããã|/ãããããããããγ ââã½ ãã ï¼ï¿£ï¿£ï¼¼ãããããããããã ï¼ã(ãã½ )ãï¾ ãï¼_ããã ããï¼¼ããããã(â) ä¸ã ï¾ ä» ã ãï¼ âï¼ï¼ âï¼ããã½ ã ä¸/ã|ãããã____ ã ãã(â)ãã .ã| ï¼__人__ï¼ãuãã}ããã|ã |ãããï¼ï¼¼ã ï¼ ) ã ã / |ãããã ã |ãï½ â´ããããï¾ããã! ã 、 ï¼(â ï¼::ï¼â ï¼âï¼¼/ã|ãã .ã |ãããããã ã }ããããï¼¼.ï¼:::::::ï¼_人_ï¼::::::::ã i' ã |ããOAuth Verifier æã«å ¥ãããã .ã ã½ããããã ã }ãããããã|ãããã ï¼wwï¼ ã ãã|ãã|ããããã ããå æ¸ã¢ã¯ã»ã¹ãã¼ã¯ã³æãã¦ãããï¼ ãã ã½ãããããããããããã ï¼¼ããã `ã¼"ããããããï¾ ããã/ããã ã 、_ï¼ã£/ ã ãããï¼¼ ï¼ããããï¼ãããï¼¼ ããã|ãããã ï¼¼--ä¸'' ã ã ã ã ã ï¼¼ ã ã |ãããã|ã½ãäºâ)、ã ã ããããã ï¼¼ ã ã ããã____ ã ãããï¼ã ã ã½ï½¤_ï¼¼ããä¸ä½ã©ããããã¨ã ãï¼ï¼ï¼ï¼ ãããï¼ï¼ âï¼}liil{ï¼âï¼ï¼¼ããã©ãã ãæç¶è¸ã¾ãããã ãï¼ï¼ï¼ï¼ ã ï¼ããã ï¼__人__ï¼ãããï¼¼ãã¢ã¯ã»ã¹ãã¼ã¯ã³æã«å ¥ããã ã㧠ã |ãããã½ã|!!il|!|!l|ã/ããã|ããæ¥æ¬ã®å½¹æ以ä¸ã«ãããã¾ãããããªããã!! ã ï¼¼ãããã|ェェェェ| ã ã ï¼ ã ã ãããï¼ï¿£ï¿£ï¿£ ï¼¼ ãï¾ï½¼ï¾ï¾ï½¼ï¾ ã ãããï¼ãâãããâãï¼¼ ã ã ï¼ ã ï¼âï¼ ãï¼âï¼ããï¼¼ãã£ã¦æã£ã¦ããããããªãã®ã¯å½¹æã§æ £ãã¦ãã ã ã |ã ãããï¼__人__ï¼ ãã ã | ã ã ï¼¼ãã ï½j |â´ ãã ãï¼ ã ãããã ã__ï¾ ã ããããã ã ï¾ ããããã|âãããã ãカï¾ï¾ï½¯.ã|âãã ããããã|âã ï¾//ããã.ï¼ï¿£ï¿£ï¿£ ï¼¼ ããããã|âä¸ããããï¼ã ï¼¼ãã ï¼ãï¼¼ãããOAuth Verifier æã«å ¥ãããã ããããã|â ããã ï¼ããï¼âï¼ ãï¼âï¼ ãï¼¼ã ã¢ã¯ã»ã¹ãã¼ã¯ã³ããã ããããã|âã ãã |ããããï¼__人__ï¼ãã ãã| ããããã|âä¸ãã ï¼¼ããã ï½ â´ããããï¼ ããããã|âä¸ãã ï¼ï¿£ï¿£ï¿£ï¿£ï¿£ï¿£ï¿£ ï¼¼ ãã ï¼ï¿£ï¿£ï¼¼ ããããã|ââ ãï¼ãã _ãããï¼¼ ãä¸ .|ââ ã|ããã ï¼ âï¼ï¼âï¼ ã ..|ââ .ã|ãããã ï¼__人__ï¼ãã. |ââ ã |ãããããï½ â´ï¾ããã|ââ .ã |ãããããã ã }ã ä¸ã|ââ .ã ã½ããããã ã }ãããã|ââãã·ã£ãï¼ ãã ã½ããããããä¸ãã ..|ââ ããã/ããã ãããï¼¼ã ã.|ââ ããã|ãããã ï¼¼ ã ä¸ã |ââ ã ã |ãããã|ã½ãäºâ)ã|ââ ã |âã ã ã¬ã©ãããã ____ ã |âãä¸ããããããï¼uãããã ï¼¼ ã |âããããã ã ï¼ ã ï¼¼,ã、ï¼ã ï¼¼ããã ã |âã.ã ã ã ï¼ ãï¼ âï¼ã ï¼â ï¼ãï¼¼ ã |âãä¸ããã|ãã '"ãï¼__人__ï¼"'ãuã| ï¼ãéããªãã§ãããã£ï¼ ã |âãããã ã ï¼¼ã ãã` â ´ ã ãï¼ ã |âããã ã ã ï¼ãã ã "`ãã ã£ãï½ã½. ã |âãä¸ ã ï¼ããããããããããããããï¼¼ ,âï¾Â´â ̄ ̄"ããyãããããããããã r、ã ã½ âäºã ,ãââ-â'´|ã ã ã ã ã ã ã | l"ã | ã |â 'ããããããã|ã ã ã ã ã ã ã l/'âã¾ ã |âä¸ããã ã ã |ã ã ã ã ã ã ã |ã¾___ソ ããããããããã___ã ãããããããï¼ãã ã ãï¼¼ ããããã ï¼ãã::ï¼¼:::ï¼::::u.ï¼¼ãããã ã.ï¼ï¿£ï¿£ï¼¼ ããããï¼ ãã<â>::::::<â>ãï¼¼ãããï¼ããã ãã.ã½ ãã ã |ã ããããï¼__人__ï¼u. ã |ãã.ï½ãããããã:::::::l ã ã ã ï¼¼ãã ãu.ï½ â´ã ãï¼ã ãï½ããããã::::::::::l ããã ã /âã½ã ãã¼â ããィã½ããã ï½ãããã:::::::::::::lã ã ã ã / ã â ã â ãã.åããå.ãã {ãuã:::::::::::::::::lããã§ã¯ãã¡ãã¸ã¢ã¯ã»ã¹ãã¦ãã ãã⦠ãããã/,ã./ã â¨ã ã½ (.ãµ.ï¼ï¼.ãµ.ï¼ãã{ãã::::::::::::::::/ãããã ãããã|ã .lå -0lå -0l } ソ {ã} ソ {ã½ãããã::::::::::::/ ããããããã¤ï¼.lï¼ï¼lï¼.ã.ï¼.ï¼ã¨â.ï¾ããã>ã:::::::::ã½ã ã  ̄ ̄ ï¾ã½ï¼¿ã½ï¾ï¾--㣠ã-ããããï¾ ï¿£ï¼ :::::::::::::::::iï¿£ ã ã ããã____ ã ãããï¼âããâï¼¼ ã ãããï¼ï¼ âï¼ ãï¼âï¼ï¼¼ ã ã ï¼::::::âï¼__人__ï¼â:::::ï¼¼ ã(ã!! ãã£ã¨ã¢ã¯ã»ã¹ãã¼ã¯ã³è²°ãããã ã!!!) ã |ããããã|râ¬-|ããããã| ã ï¼¼ ãã ã `ã¼'´ ã ã ï¼
ã¨ï¼ããããã§ãã£ã¨ã¢ã¯ã»ã¹ãã¼ã¯ã³ã®åå¾ã§ãï¼
#!/usr/bin/env python # -*- coding: utf-8 -*- import time, random, urllib, urllib2, cgi, hmac, hashlib consumer_key = 'æåã«ä½¿ã£ããã¤' consumer_secret = 'æåã«ä½¿ã£ããã¤' oauth_token = 'ãªã¯ã¨ã¹ããã¼ã¯ã³åå¾ã®æã«åå¾ãã OAuth Token' oauth_token_secret = 'ãªã¯ã¨ã¹ããã¼ã¯ã³åå¾ã®æã«åå¾ãã OAuth Token Secret' oauth_verifier = 'ãã©ã¦ã¶ã«è¡¨ç¤ºããã OAuth Verifier' # Access Token 㯠GET ã§åå¾ method = 'GET' # Access Token åå¾ã® URL access_token_url = 'http://twitter.com/oauth/access_token' # å¿ é ãã©ã¡ã¼ã¿ãæºå params = { "oauth_consumer_key": consumer_key, # WEB ã§ç»é²ãã oauth consumer key "oauth_signature_method": "HMAC-SHA1", # æå·ã®ã¢ã«ã´ãªãºã "oauth_timestamp": str(int(time.time())), # unixtime "oauth_nonce": str(random.getrandbits(64)), # ã©ã³ãã æåå "oauth_version": "1.0" # ãã¼ã¸ã§ã³çªå· } # ãã©ã¡ã¼ã¿ã«å ç¨åå¾ããå¤ã追å params['oauth_token'] = oauth_token params['oauth_verifier'] = oauth_verifier # ãã©ã¡ã¼ã¿ãã½ã¼ããï¼URIã¨ã³ã³ã¼ããã key=value ã®å½¢ã«ã㦠& ã§ç¹ãã¾ã params_str = '&'.join(['%s=%s' % (urllib.quote(key, ''),urllib.quote(params[key], '')) for key in sorted(params)]) # ã¡ã½ããï¼URIã¨ã³ã³ã¼ããã URL,ä¸ã§ä½ã£ããã©ã¡ã¼ã¿æååã & ã§ç¹ãã¾ã message = '%s&%s&%s' % (method,urllib.quote(access_token_url,''), urllib.quote(params_str,'')) # consumer_secret 㨠oauth_token_secret ãå ã«ãã¼ãä½æãã¾ã key = "%s&%s" % (consumer_secret, oauth_token_secret) # ãã¼ãå ã« message 㧠hmac ãä½æãã¾ã signature = hmac.new(key, message, hashlib.sha1) # ãã®ãã¤ã¸ã§ã¹ãã base64 ã§ã¨ã³ã³ã¼ããï¼ä½è¨ãªç©ºç½ãåãé¤ãã¾ã digest_base64 = signature.digest().encode("base64").strip() # ä½æãããã¤ã¸ã§ã¹ãããã©ã¡ã¼ã¿ã«è¿½å ãã¾ã params['oauth_signature'] = digest_base64 # ãããã« Authorization:OAuth ã追å ãã¾ã opener = urllib2.build_opener() opener.addheaders = [('Authorization','OAuth')] # ä½æãããã©ã¡ã¼ã¿ã GET ã®ãã©ã¡ã¼ã¿ã¨ãã¦è¿½å ãã¾ã _url = access_token_url + '?' + urllib.urlencode(params) # ãã® URL ã«ã¢ã¯ã»ã¹ã, ResponseBody ãèªã¿è¾¼ã¿ã¾ã result = opener.open(_url).read() # ãã¼ã¹ãã¾ã data = cgi.parse_qs(result) # åå¾ã§ãã¾ãã!! print data #{'oauth_token_secret': ['xxxx'], 'user_id': ['xxxx'], 'oauth_token': ['xxxx'], 'screen_name': ['yaruo']} # oauth_token ãã¢ã¯ã»ã¹ãã¼ã¯ã³
æåããã¨ï¼ã¢ã¯ã»ã¹ãã¼ã¯ã³ã®ä»ã«ãã¦ã¼ã¶åãªã©ããããã¾ã.
ãªã¯ã¨ã¹ããã¼ã¯ã³ã®ã¨ããã§ã説æãã¾ãããï¼
ããã§åºã oauth_token 㨠oauth_token_secret ãä¿åãããããã¨ï¼
ããä¸åº¦ãªã¯ã¨ã¹ããã¼ã¯ã³ã®åå¾ããã«ãªãã®ã§æ³¨æãã¦ãã ããï¼
ã¤ã¾ãï¼ãã®ããã°ã©ã ãé£ç¶ã§å®è¡ãã㨠2 度ç®ä»¥éã¯ã¨ã©ã¼ã«ãªãã¾ãï¼
ã¢ã¯ã»ã¹ãã¼ã¯ã³ã使ã£ã¦å®éã« Twitter ã«ã¢ã¯ã»ã¹ãã¦ã¿ã
ã ã ããã____ ã ãããï¼âããâï¼¼ ããã£ã±ãè¦å´ãããã© ãããï¼ï¼ âï¼ ãï¼âï¼ï¼¼ ãã¢ã¯ã»ã¹ãã¼ã¯ã³æã«å ¥ãããï¼ï¼ ã ï¼::::::âï¼__人__ï¼â:::::ï¼¼ ãè¦å´ãå ±ããããï¼ï¼ ã |ããããã|râ¬-|ããããã| ã ï¼¼ ãã ã `ã¼'´ ã ã ï¼ ã ã ããã____ ã ãããï¼âããâï¼¼ ãããï¼ï¼ ï¼ï¼ ãï¼ï¼ï¼ï¼¼ããã£ããã¢ã¯ã»ã¹ãã¼ã¯ã³ä½¿ã£ã¦ ã ï¼::::::âï¼__人__ï¼â:::::ï¼¼ãTwitter ã«ããã£ã±ããã£ã¦æ¸ãè¾¼ãã!! ã |ãããã/| | | | |ãããã ã| ã ï¼¼ã (ã`ã¼â'´,ãã ãï¼
ã§ã¯ï¼å®éã«ã¢ã¯ã»ã¹ãã¦ã¿ã¾ãããï¼
ä»å㯠twitter ã«çºè¨ããããµã³ãã«ã§ãï¼
#!/usr/bin/env python # -*- coding: utf-8 -*- import time, random, urllib, urllib2, cgi, hmac, hashlib, commands # çºè¨å 容 text = u'ãã£ã±ã' consumer_key = 'æåã«ä½¿ã£ããã¤' consumer_secret = 'æåã«ä½¿ã£ããã¤' oauth_token = 'ã¢ã¯ã»ã¹ãã¼ã¯ã³åå¾ã®æã«åå¾ãã OAuth Token' oauth_token_secret = 'ã¢ã¯ã»ã¹ãã¼ã¯ã³åå¾ã®æã«åå¾ãã OAuth Token Secret' # çºè¨ã® URL update_url = 'http://twitter.com/statuses/update.json' # å¿ é ãã©ã¡ã¼ã¿ãæºå params = { "oauth_consumer_key": consumer_key, # WEB ã§ç»é²ãã oauth consumer key "oauth_signature_method": "HMAC-SHA1", # æå·ã®ã¢ã«ã´ãªãºã "oauth_timestamp": str(int(time.time())), # unixtime "oauth_nonce": str(random.getrandbits(64)), # ã©ã³ãã æåå "oauth_version": "1.0" # ãã¼ã¸ã§ã³çªå· } # ãã©ã¡ã¼ã¿ã«å¿ è¦ãªå¤ã追å params['oauth_token'] = oauth_token params['status'] = text # ãã©ã¡ã¼ã¿ã®å¤ã unicode ã ã£ããã¨ã³ã³ã¼ããã¾ã for k,v in params.items(): if isinstance(v, unicode): params[k] = v.encode('utf8') # ãã©ã¡ã¼ã¿ãã½ã¼ããï¼URIã¨ã³ã³ã¼ããã key=value ã®å½¢ã«ã㦠& ã§ç¹ãã¾ã params_str = '&'.join(['%s=%s' % (urllib.quote(key, ''),urllib.quote(params[key], '~')) for key in sorted(params)]) # ã¡ã½ããï¼URIã¨ã³ã³ã¼ããã URL,ä¸ã§ä½ã£ããã©ã¡ã¼ã¿æååã & ã§ç¹ãã¾ã message = '%s&%s&%s' % ('POST',urllib.quote(update_url,''), urllib.quote(params_str,'')) # consumer_secret 㨠oauth_token_secret ãå ã«ãã¼ãä½æãã¾ã key = "%s&%s" % (consumer_secret, oauth_token_secret) # ãã¼ãå ã« message 㧠hmac ãä½æãã¾ã signature = hmac.new(key, message, hashlib.sha1) # ãã®ãã¤ã¸ã§ã¹ãã base64 ã§ã¨ã³ã³ã¼ããï¼ä½è¨ãªç©ºç½ãåãé¤ãã¾ã digest_base64 = signature.digest().encode("base64").strip() # ä½æãããã¤ã¸ã§ã¹ãããã©ã¡ã¼ã¿ã«è¿½å ãã¾ã params['oauth_signature'] = digest_base64 # ãããã«ã¯ API ã«å¿ è¦ãªãã©ã¡ã¼ã¿ã¯å«ããªãã®ã§æ¶ãã¾ã del params['status'] # ãã©ã¡ã¼ã¿ãã½ã¼ããï¼URIã¨ã³ã³ã¼ããã key=value ã®å½¢ã«ã㦠, ã§ç¹ãã¾ã header_params_str = ",".join(["%s=%s" % (urllib.quote(k,''), urllib.quote(params[k],'~')) for k in sorted(params)]) print header_params_str # ãããã« Authorization:OAuth + ä¸ã§ä½æããæååã追å ãã¾ã opener = urllib2.build_opener() opener.addheaders = [('Authorization','OAuth %s' % header_params_str)] # ãã¨ã¯æ®éã« API ãå¼ã³åºãã¾ã result = opener.open(update_url,urllib.urlencode( {'status':text.encode('utf-8')})).read()
ãã£ã±ã
ããããã ._ ãããã ã ï¼¼ã½,ã,ã ãããã ããã`''|/ï¾ ãããã ãããã.| ãããã _ããã| ãããã ï¼¼ï½ã½ã| ãããã ãï¼¼, V ãããã ã ã `L,,_ ããããã ãã |ã½ï½¤)ã ,、 ããããããã /ããããã½Yï¾ ãããããã /ããããr''ã½ã.| ãããããã|ããããã`ï½°-ã½|ã® ãããããã|ããããããã`| ãããããã|.ããããããã | ããããããã½ããããããã| ããããããã ã½ï¼¿ï¼¿ï¼¿_ï¾ãã ããã ãããã ãããï¼_ã 'ãã½_ï¼¼ ããããããï¼ï¼â¡ï¼ ã ï¼â¡ï¼ï¼¼ ãããã ï¼::::::âï¼__人__ï¼â::::: ï¼¼ ããããã ãããã |ããããã|râ¬-|ããããã|ãããã ã ãããã ï¼¼ ãã ã `ã¼'´ ã ã ï¼ ãããããï¼ããããããããããï¼¼ ãããã (ã |ããããããããã |ã ) ãããããï¼¼|ããããÑãããã|ï¼ ãããããã (ãããã,,,,ããã ,ï¾ ãããããããï¼¼ã 、(ï¼µ)ï¾ãã ãããããããã ï¼¼ï¼ã ï¼ããããããããããããâ¼ã½ã -|râ、.ãï¾ã| ãããããããã ï¼ã ï¼ï¼¼ããããããããããããï½â)ã./|ã_ï¾ã __ï¾ã ããããã ââ__ï¼ï¼¿ï¼¿ï¼
çµè« ï¼ ããã©ãããããã®ã§ï¼ã©ã¤ãã©ãªä½¿ãã¾ããã