CoreTweetã§OAuthã使ã£ãèªè¨¼ããããµã³ãã«ãè¦å½ãããªãã£ãã®ã§ããã°ã«æ¸ãã¦ããã¾ãã
å人çãªçµç·¯
Webãµã¤ãã«ä½ãã¡ãã»ã¼ã¸ãæ稿ããã¨åæã«ãã®å 容ããã¤ã¼ããããããå ´åãã©ãããã®ãè¯ãã§ãããã
ãã®ã¦ã¤ã³ãã¦ã使ãã°ç°¡åï¼ããï¼ã§ãããå 容ãå¤æ´ã§ãã¦ãã¾ãã®ã§ã¡ãã£ã¨éãæ°ããã¾ãã
çµæãWebãµã¤ãï¼ã¢ããªã±ã¼ã·ã§ã³ï¼ãèªè¨¼ãã¦ããã£ã¦ãã¡ãã»ã¼ã¸ã®æ稿ããä¸é£ã®æµãã§ãã¤ã¼ããããã®ãè¯ãããããªããã¨ããçµè«ã«è³ãã¾ããã
ããã§ã以åæ¸ããASP.NET Identityã使ã£ã¦ãã¤ã¼ãããè¨äºã使ãã°è¯ãããªã¨ãæã£ãã®ã§ããããã®æ¹æ³ã ã¨ãµã¤ãã«ãã¦ã¼ã¶ã¼ç»é²ãã¦ããããªãã¨ãããªããã®ã§å©ç¨ããå´ã¨ãã¦ã¯ãæ軽ãããªãã¨æãã¾ãã
çµè«ï¼CoreTweetã使ã£ã¦ã¢ããªãèªè¨¼ããã
çµå±ãCoreTweetã使ã£ã¦Twitterèªè¨¼ããä»çµã¿ãå®è£ ãããã¨ã«ãã¾ããã
å®è£ ãµã³ãã«
ã¨ããããã§ããµã³ãã«ã³ã¼ãã§ãã
äºåæºåã¨ãã¦ãTwitter Application Managementãµã¤ãã«ã¦Twitterã¢ããªã±ã¼ã·ã§ã³ã®ç»é²ãå¿ è¦ã§ãããã®è¾ºã¯ã°ã°ã£ã¦ããããBingã£ã¦ããããã°ããããæ å ±ãåºã¦ããã¨æãã¾ãã®ã§å²æãã¾ãã
CoreTweetã®ã¤ã³ã¹ãã¼ã«ã¯ããã±ã¼ã¸ããã¼ã¸ã£ã³ã³ã½ã¼ã«ããInstall-Package CoreTeweet
ã§è¡ãã¾ããç°¡åã§ããã
èªè¨¼ãã¼ã¸ã®å¼ã³åºã
ç»é²ããTwitterã¢ããªã±ã¼ã·ã§ã³ç¨ã«çºè¡ãããAPIãã¼ã¨APIãã¼ã·ã¼ã¯ã¬ããã使ç¨ãã¦ãOAuthSessionã¤ã³ã¹ã¿ã³ã¹ãåå¾ãã¾ãã
ãã®æãèªè¨¼å®äºå¾ã«ãªãã¤ã¬ã¯ãããURLãæå®ãã¾ãããã®ãã©ã¡ã¼ã¿ãæå®ããªãã£ãå ´åãèªè¨¼å¾ã«PINã³ã¼ãã表示ããã¾ãã
ã¾ãããã®ã¤ã³ã¹ã¿ã³ã¹ã«æ ¼ç´ããã¦ããæ å ±ãå¾ã å¿ è¦ã«ãªãã®ã§ãã»ãã·ã§ã³æ å ±ã¸æ ¼ç´ãã¦ããã¾ãã
ããã¦ãèªè¨¼ãã¼ã¸ã«ãªãã¤ã¬ã¯ããã¾ãã
public class AuthController : Controller { public ActionResult Twitter(HomeIndexViewModel model) { var oAuthSession = OAuth.Authorize("{TwitterApiKey}", "{TwitterApiKeySecret}", "http://mydomain.com:63543/AuthCallback/Twitter"); // ã»ãã·ã§ã³æ å ±ã«OAuthSessionã®å 容ãä¿å Session["OAuthSession"] = oAuthSession; return Redirect(session.AuthorizeUri.OriginalString); } }
èªè¨¼ãã¼ã¸
è¦æ £ãããããªç»é¢ã«ãªãã¤ã¬ã¯ããããã®ã§ãé£æºã¢ããªãèªè¨¼ããã¾ãã
ããã¨ãäºåã«æå®ããURLã¸ãªãã¤ã¬ã¯ãããã¾ãã
ãªãã¤ã¬ã¯ãå ã§ã¢ã¯ã»ã¹ãã¼ã¯ã³ã®åå¾
ãªãã¤ã¬ã¯ãæã«ãã©ã¡ã¼ã¿ã¨ã㦠oauth_token 㨠oauth_verifier ã渡ããã¾ãã
ãã¼ã¯ã³ãåå¾ããéãèªè¨¼ãã¼ã¸ã®URLãåå¾ããéã®æ å ±ã¨ oauth_verifier ã使ç¨ãã¾ãã
public class AuthCallbackController : Controller { public ActionResult Twitter(string oauth_token, string oauth_verifier) { // OAuthSessionã¤ã³ã¹ã¿ã³ã¹ã復å var oAuthSession = Session["OAuthSession"] as OAuth.OAuthSession; var token = session.GetTokens(oauth_verifier); // token ãã AccessToken 㨠AccessTokenSecret ãæ°¸ç¶åãã¦ããã¨ãã // ã»ãã·ã§ã³æ å ±ã«æ ¼ç´ãã¦ããã°æ¯åèªè¨¼ããªãã¦è¯ããã return Redirect("~/"); } }
ãã¨ãã
ããã ãã®ãã¨ããã®ã«çµæ§æéãé£ã£ã¦ãã¾ãã¾ãããè¨èã§ã¯ãOAuth使ã£ã¦èªè¨¼ããã°è¯ãããããã®ã²ã¨è¨ã§æ¸ã¿ã¾ããããããã£ã¦ã¿ãã¨ããã£ã¦ããªãäºãããããã¨æ確ã«ãªã£ã¦ãã¾ãã
ãããããã®ãããã§æ¬¡ããã¯ããããã£ã¨æãåã訳ã§ãããããã¯ãããããæãåããã¨ããªãã¨ãªãã¨æãã1é±éã§ããï¼ãã
追è¨ï¼2016/08/05ï¼
ä¸ã®ãµã³ãã«ã³ã¼ãã§ã¯Session使ã£ã¦ã¾ãããã©ãTempData使ã£ãæ¹ãããã¨æãã¾ãã
TempDataãã»ãã·ã§ã³æ å ±ä½¿ã£ã¦ãã¼ã¿ãä¿æãã¾ãããåºæ¬çã«ä¸åº¦èªã¿åºãããã»ãã·ã§ã³æ å ±ããã¯åé¤ããã¾ããç¡é§ãªãªã½ã¼ã¹ããã¤ã¾ã§ãä¿æããªãæ¹ãæ°æã¡è¯ãã§ãããã