èªåã®æ¢ãæ¹ãæªãã®ãã
Twitterã«OAuthèªè¨¼ãã¦ã¤ã¶ããAIR/Flex/ActionScript3ã®ãµã³ãã«ãè¦ã¤ããããåè¦å
«è¦ãã¾ããã
ãªãã ãtwitterã§AS3ãé©å½ãªæ±ããåãã¦ãããããªå°è±¡ãâ¦â¦ã
ã¨ãããã¨ã§ãtwitterã«æ稿ãããµã³ãã«ã³ã¼ããã¡ã¢æ¸ãã
æºå
OAuthèªè¨¼ãã¦ãAccess Tokenãåå¾ãã¦ããå¿ è¦ãããã¾ããååã®ã¨ã³ããªãåç §ã
Access Tokenã¯ãã¦ã¼ã¶ãé£æºã¢ããªã®èªå¯ãåãæ¶ããªãéãæå¹ã§ãã
èªè¨¼ããã
BASICèªè¨¼ã¨åãããã«ãHTTPãããã«'Authorization'ããããè¨è¿°ããå¿ è¦ãããã¾ãããã®ããããä½ãã®ãé常ã«é¢åãããã§ãã
ååã®ã¨ã³ããªåæ§ãiotashanæ°ã®ã©ã¤ãã©ãªãç¨ãã¾ãã
consumer tokenã«ã¯ã¢ããªã®Consumer tokenãã
access tokenã«ã¯èªå¯ãããAccess tokenãæå®ãã¾ãã
var oauthReq:OAuthRequest = new OAuthRequest("POST", "http://api.twitter.com/1/statuses/update.xml", { oauth_version: "1.0", status: ã¤ã¶ããæ¬æ},consumer token, access token);
var auth:URLRequestHeader = oauthReq.buildRequest(new OAuthSignatureMethod_HMAC_SHA1, OAuthRequest.RESULT_TYPE_HEADER);
trace(auth.toString()); //Authorizationããããåºåããã
ã¨ã¦ããã£ããæ¸ãã¦ã¡ãã¿ã·ã¡ãã¿ã·â¦â¦ã¨è¨ãããã¨ãããªã®ã§ããã
ä¸è¨ã®ã³ã¼ãã¯ä¸æãåãã¾ããã
åå ã¯ãåãã©ã¡ã¼ã¿ã®åºç¾é åºã«ããã¾ãã
oauth_ã§å§ã¾ããã©ã¡ã¼ã¿ã¯ããã©ã¡ã¼ã¿åã®è¾æ¸é ã§æé ã½ã¼ãããã¦ããå¿
è¦ãããã¾ããã
æ®å¿µãªããä¸è¨ã³ã¼ãã§ã¯ã½ã¼ãããã¾ããã
åºæ¥ä¸ãã£ãRequestãtraceãªã©ã§ç¢ºèªãã¦ã¿ã¦ãã ããã
ä¿®æ£æ¹æ³ã¯ããã¤ãèããããã¨æãã¾ãããç§ãåã£ãçµæ§å¼·å¼ãªæ¹æ³ãæ¸ãã¦ããã¾ãã
var oauthReq:OAuthRequest = new OAuthRequest("POST", "http://api.twitter.com/1/statuses/update.xml", oauth_version: "1.0", status: ã¤ã¶ããæ¬æ}, Consumer token, Access token);
var auth:String = oauthReq.buildRequest(new OAuthSignatureMethod_HMAC_SHA1, OAuthRequest.RESULT_TYPE_POST);
auth = auth.split("&").join("\",");
auth = auth.split("=").join("=\"");
auth = auth.replace(/,status=.*/, "");
var authHeader = new URLRequestHeader("Authorization", "OAuth " + auth);
ããã§çæãããAuthorizationãããã¯ã以ä¸ã®ããã«ãªãã¯ãã§ãã
Authorization: OAuth oauth_consumer_key="Consumer key",oauth_nonce="nonce",oauth_signature="signature",oauth_signature_method="HMAC-SHA1",oauth_timestamp="10æ¡ã®timestamp",oauth_token="Access token",oauth_version="1.0"
è¾æ¸é ã«ãªã£ã¦ãã¾ãããï¼
èªè¨¼ãããçæã«ãã¤ã¶ããæ¬æãç¨ãã¦ããçç±ã¯ã
ãã¡ãã«æ¸ããã¦ããéãã
oauth_signatureã®çæã«ãã¤ã¶ããæ¬æãç¨ãã¦ããããã§ãã
ãªããã¤ã¶ããæ¬æã¯ãURLEncodeããªãã§OAuthRequestã«æ¸¡ãã¾ãã
ãªããªãã°ãOAuthRequestå
é¨ã§URLEncodeããããããå¤éã«ã¨ã³ã³ã¼ãããã¦ãã¾ãã¾ãã®ã§ã
å®éã®ã¤ã¶ããã¨ãèªè¨¼ã§ç¨ããã¤ã¶ããæ¬æãç°ãªããèªè¨¼ãéããªããªãã¾ãã
HTTPéä¿¡ãªãã¸ã§ã¯ã
HTTPServiceãURLLoaderãªã©ãå©ç¨ããå ´åãAuthorizationãå«ãä¸é¨ã®ã«ã¹ã¿ã ããããå©ç¨ã§ãã¾ããã
as3httpclientlibã使ãã¾ãããã
BASICèªè¨¼ã§ãAuthorizationããããå¿ è¦ãªã®ã§ã
ã©ã¡ãã使ãã«ãã¦ããHTTPServiceã¯ä½¿ãã¾ããã
statusã®è¨è¿°
次ã«ãstatus(ã¤ã¶ããæ¬æ)ãæ¸ãä½ç½®ã§ãããBASICèªè¨¼ã®æã¯ã
http://api.twitter.com/1/statuses/update.xml?status=test
ã®ããã«ãURLãã©ã¡ã¼ã¿ã§è¨è¿°ã§ãã¾ãããã
ããã¯ä»ã§ããä¸éå端ã«çãã¦ãã¾ãã
ã¨ããã®ã¯ãASCIIæåã®ã¿ã®POSTãªãããã§ããã¾ããã
æ¥æ¬èªãPOSTããããã®ãªãã
500 internal server errorãè¿ã£ã¦ãã¾ãã
URLEncodeãã¦ããã¡ã§ãã
ä¸ã®ä¾ãªãASCIIã®ã¿ãªã®ã§æ£å¸¸ã«ãã¹ãããã¾ãã
ãtestããããã¹ããã«å¤ãã¦URLEncodeãããã®ãããªãã¡ã
status=%E3%83%86%E3%82%B9%E3%83%88
ã¨ããã¨ã失æãã¾ã(500: internal server error)ã
ãããªããã§ãstatusã¯Bodyã«è¨è¿°ãã¦ããå¿ è¦ãããã¾ãã
ä¸éå端ã«çãã¦ãã¦ãããããããããªã¾ããã
as3httpclientlibã®HttpClientã§POSTããæ¹æ³
ããã¯ã½ã¼ã¹ã³ã¼ãè¦ãæ¹ãæ©ãã§ãããã
var req:HttpRequest = new Post();
req.addHeader("Authorization", ä¸ã§ä½ã£ãAuthorizationæåå);
req.setFormData([{ name: "status", value: ã¤ã¶ããæ¬æ }]);
var client:HttpClient = new HttpClient();
client.request(new URI("http://api.twitter.com/1/statuses/update.xml"), req);
注ï¼ä¸è¨ã³ã¼ãã¯ç°¡ç¥åãã¦æ¸ãã¦ããã¾ããçµæãåãåããããªããªã¹ãã¼ãç»é²ãã¾ãããã
ã¡ãã»ã¼ã¸ããã£ã¼ã¸ã®æ¸ãè¾¼ã¿ã«ã¯ãå¿
ãsetFormDataãå©ç¨ããããã«ãã¾ãããã
HttpRequest.bodyã«ç´æ¥æ¸ãè¾¼ãã¨ãçç±ã¯ä¸æã§ãããéä¿¡ããã³ã°ã¢ãããã¾ãã
setFormDataã®valueã¯ãèªåçã«URLEncodeããã¾ãã®ã§ã
URLEncodeããã«ãçã§æ¸¡ãããã«ãã¾ãããã
ãªã¿ã¼ã³ã³ã¼ã
401ãè¿ã£ã¦ããããAuthorizationã®å¤ï¼ç¹ã«ã·ã°ããã£ï¼ï¼ãééã£ã¦ããå¯è½æ§ãé«ãã§ãã500ãè¿ã£ã¦ãããããªã¯ã¨ã¹ãã®æ§æãééã£ã¦ããå¯è½æ§ãããã¾ãã
401ã¯åå ãçªãæ¢ããããã§ããã500ã¯ææã§ãã
ãªãã ãé常ã«è¦å´ãããã§ããã
ã¿ããªãã£ãã解決ãã¦ããã§ãããâ¦â¦ï¼
ä»ã®è¨èªã¯ã©ã¤ãã©ãªãå
å®ãã¦ããã®ã«ã
AS3ããªããä¸éã ã£ã¦ãããããã®ã§ããããã©ã
HTTPServiceã¯Authorizationããããè¨è¿°ã§ããªãã¨ããAS3ç¹æã®åé¡ãããã¾ããã