ãã¾ã«ã¯ããã°ãæ¸ã
ãã夫㨠Python ã§å¦ã¶ Twitter ã® OAuth - Yoshioriã®Blog
ããã¾ã¨ãï¼
ã¯ã¦ãã³ã¡ã³ãããå¼ç¨
id:agricola Python, ããã ãªã¹ãå å ã®ä½¿ãæ¹ã«ç¾ããªããªãâ¦â¦ãè¾æ¸ã®ãã¼ã¨å¤ãããã¼ã§ä¸¦ã¹æ¿ãã¦åãåºãã¦=ã§é£çµãããªã
['='.join((k, v)) for k, v in sorted(d.items(), key = lambda x: x[0])]ã¨ãæ¸ãæ°ãããã
ãããªé¤ã§ä¿ºæ§ãé£ããã¯ãã¼ã¼ã¼ã¼ã¼ã¼ã£
>>> ['='.join((k, v)) for k, v in sorted(d.items(), key = lambda x: x[0])] ['a=k', 'b=j', 'c=i', 'd=h', 'e=g', 'f=f', 'g=e', 'h=d', 'i=c', 'j=b']
ãµãããªãã ãã®ã´ãã´ãã¨ãããªããã®ããã£ã¤ããã³ã¼ãã¯ã
>>> ['='.join((k, d[k])) for k in sorted(d)] ['a=k', 'b=j', 'c=i', 'd=h', 'e=g', 'f=f', 'g=e', 'h=d', 'i=c', 'j=b'] >>> ['%s=%s' % (k, d[k]) for k in sorted(d)] ['a=k', 'b=j', 'c=i', 'd=h', 'e=g', 'f=f', 'g=e', 'h=d', 'i=c', 'j=b'] >>> ['%s=%s' % p for p in sorted(d.items())] ['a=k', 'b=j', 'c=i', 'd=h', 'e=g', 'f=f', 'g=e', 'h=d', 'i=c', 'j=b']
ã©ãã§ãOKãåãªã2çªç®ã§æ¸ãããªãæåã¯100%ã¤ã³ã¼ã«ã¨ããããã§ã¯ãªãã¦ã"%s=%s"ã®æ¹ã¯è¾æ¸ã®å¤ãstrã§ãªãã¦ãã¨ã©ã¼ã«ãªããªãã
ã§ãã¾ããOAuthãã³ã¼ãä»ãã§è§£èª¬ãããã¨ãã¿ããªã«ä¸ãã価å¤ã«æ¯ã¹ãã°ã1è¡ã®ã³ã¼ããã©ãæ¸ãããããããã ãªãã¦1000åã®1ã®ä¾¡å¤ããªãããªã¼ãã¤ã¾ãYoshiori GJ!!!
P.S. ã¤ãã³ã¡ã³ãæ¬ã®ã³ã¼ãã ãè¦ã¦ã¤ã£ããã§ããæ°ã¥ãããã©ãOAuthã®ã©ãã«ãã½ã¼ããã¦ããçµåãããè¾æ¸ããåºã¦ãããã ï¼
id:showyou OAuthé¢é£ã®ãã©ã¡ã¼ã¿(oauth_signature_methodã¨ãoauth_nonceã¨ã)ãéãã¨ãã«ã¯ãã©ã¡ã¼ã¿ãã½ã¼ãããç¶æ ã§éããªãã¨ãã¡
ãªãã»ã©ãé çªãå¤ããã¨ããã·ã¥å¤ãå¤ãã£ã¦ãã¾ãããããªã
id:agricola Python, ãã»ã urllib.quote()ã§ã¯ã©ã¼ãããæååã=ã§é£çµãã¦ãã®ã«%使ãã°æåå以å¤ãOKã¨ã¯çå¤ããªãå ã³ã¼ãã®æ義ã¯å¦å®ããªããããªã¹ãå å ã§å¯èªæ§ãä½ä¸ããã¦æå·§ã«èµ°ã£ãã®ã«ç¾ãç¡ãã®ã¯ããã ããªãã
(ã¤Ð´â)ã´ã·ã´ã· (ï¼ï¾Ð´ï¾) (ã¤Ð´â)ã´ã·ã´ã·
æåã100%ã¤ã³ã¼ã«ãããªãããã£ã¦æ¸ããã ããªã®ã«çå¤ãã£ã¦è¨ãããããµããã¯ã¦ãã«æ¸ãã¦ãã£ãã³ã¼ãã¨ç価ãªã³ã¼ããå è¨äºã®ã©ããªã®ãããããªãã£ãã®ã§ã¯ã¦ãã«æ¸ããã¦ããã³ã¼ããåçã§ããå ¥åãªãä½ã§ãåçã§ããããã«æ¸ããã¨ããã ãã®è©±ãæ¥æ¬èªãããããããªããã©ããªã¹ãå å ã§å¯èªæ§ãä½ä¸ããã¦æå·§ã«èµ°ã£ãã®ã«ç¾ãç¡ããã£ã¦ããã¡ããåã®ãã¨ãããªãããï¼èªèã³ã¡ã³ããã¼ã
æãã£ãããã転è¼(æ¹è¡è©°ããã誤æ¤ç´ãããè¨èè£ã£ãããã¦ããã¾ã):
tokibito: ã¨ããã§å å 表è¨ã®å è¨äºã®è©²å½ã®ã³ã¼ãã£ã¦
ããï¼
# ãã©ã¡ã¼ã¿ãã½ã¼ããï¼URIã¨ã³ã³ã¼ããã key=value ã®å½¢ã«ã㦠& ã§ç¹ãã¾ã
params_str = '&'.join(['%s=%s' % (urllib.quote(key, ''),urllib.quote(params[key], ''))
for key in sorted(params)])
西尾泰å: ãããªã®ï¼ã ã£ããåã®æ¸ããã³ã¼ãã¨ã»ã¼åããããªãï¼ãããæ·»åãã¦(quoteã¨ãããã¦ã¦)ã¯ã¦ãã³ã¡ã³ãã®ããã«ãªã£ã¦ãã®ï¼ããã£ã¦quoteãã¦ããã綺éºã«ãªã£ãããããªãã®â¦
tokibito: ããããurlencodeã§ãããããªæ°ããã
西尾泰å: urlencodeã¯å¿ãããããªããããããã®æ¨æºã©ã¤ãã©ãªã«ãã§ã«ãããããªæ°ã¯ãããurlencodeãã
tokibito: urllib.urlencode
西尾泰å: >Convert a mapping object or a sequence of two-element tuples to a âpercent-encodedâ string
西尾泰å: ããã§ãããããã
tokibito: ã½ã¼ãããã¿ãã«ã渡ãã¦ãããããé åºä¿è¨¼ä»ãã®è¾æ¸ä½¿ã£ã¦ãããããæ¬æ°ã§æ¸ããªããããã
西尾泰å:
ãããã£ããã¨ã¯ãããªã®ï¼
>>> urllib.urlencode([(k, d[k]) for k in sorted(d)])
'a=k&b=j&c=i&d=h&e=g&f=f&g=e&h=d&i=c&j=b'
tokibito: ã ã¨æããã ãã©ã©ãã ãããããããå å 表è¨ã«é¢ããææãè足ãªæãã
西尾泰å: ã£ã¦ãããå ã³ã¼ãã®ã©ãã«å¯¾ããææãªã®ãèªãã§ãªãã£ããã ãã©ãå ã³ã¼ããå¤ã«æ·»åããã®ãåãå ã³ã¼ãè¦ãã«æ·»åãããå ã³ã¼ãã¨ã»ã¼åãã«æ»ã£ãã£ã¦ãã¨ï¼
tokibito: ãããªæ°ããã
ãã£ããç¶ã
yoshiori: urllib.urlencodeã¯ãé§ç®ã ããã¹ãã¼ã¹ã + ã«ãã¡ããããã¨ãå¤ã®ææã§å¤æãããã ãã©ã ~ ãã¨ã¹ã±ã¼ããã¡ãã ã
yoshiori: http://tools.ietf.org/html/rfc5849#section-3.6
yoshiori:
>>> urllib.urlencode({'foo':'bar hoge'})
'foo=bar+hoge'
>>> urllib.quote('bar hoge','~')
'bar%20hoge'
ãã¨ã¯ã¦ãã³ã¡ã³ãã§ã:
id:mattn urlencodeã¯å é¨ã§quote_plus使ãã®ã§%20ã®çµæãéã£ã¦èªè¨¼å¤±æããããããªãããªããã¨ãquoteã§ã¡ããã¨ãããªã urllib.quote(s, safe='~') ãã
thanks!