{"id":394,"date":"2011-12-08T01:08:36","date_gmt":"2011-12-07T16:08:36","guid":{"rendered":"http:\/\/www.whoocus.com\/blog\/wordpress\/?p=394"},"modified":"2011-12-08T01:08:36","modified_gmt":"2011-12-07T16:08:36","slug":"ruby-on-rails-twitter4r-%e3%81%a7%e3%81%aeoauth%e8%aa%8d%e8%a8%bc%e5%87%a6%e7%90%86%e3%81%a8%e3%83%84%e3%82%a4%e3%83%bc%e3%83%88%e3%81%ae%e5%9f%ba%e6%9c%ac","status":"publish","type":"post","link":"https:\/\/www.blowfish.co.jp\/?p=394","title":{"rendered":"Ruby on Rails &#038; Twitter4R \u3067\u306eOAuth\u8a8d\u8a3c\u51e6\u7406\u3068\u30c4\u30a4\u30fc\u30c8\u306e\u57fa\u672c"},"content":{"rendered":"<p>\u56db\u82e6\u516b\u82e6\u3057\u3066\u3001\u3088\u3046\u3084\u304f\u8a8d\u8a3c\u51e6\u7406\u3068\u30c4\u30a4\u30fc\u30c8\u307e\u3067\u305f\u3069\u308a\u7740\u3044\u305f\u3002<\/p>\n<p>[ruby]<br \/>\n#coding: utf-8<\/p>\n<p>require &#8216;twitter&#8217;<br \/>\nrequire &#8216;oauth&#8217;<\/p>\n<p>class OauthController &lt; ApplicationController<\/p>\n<p>  #&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n  # Twitter\u8a8d\u8a3c\u753b\u9762\u9077\u79fb<br \/>\n  #&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n  def connect<br \/>\n    #\u30ea\u30af\u30a8\u30b9\u30c8\u30c8\u30fc\u30af\u30f3\u53d6\u5f97<br \/>\n    consumer = OAuth::Consumer.new(&quot;\u30b3\u30f3\u30b7\u30e5\u30fc\u30de\u30ad\u30fc&quot;, &quot;\u30b3\u30f3\u30b7\u30e5\u30fc\u30de\u30b7\u30fc\u30af\u30ec\u30c3\u30c8&quot;, :site =&gt; &quot;http:\/\/twitter.com&quot;)<br \/>\n    request_token = consumer.get_request_token(:oauth_callback =&gt; &quot;http:\/\/\u30b3\u30fc\u30eb\u30d0\u30c3\u30afURL\/oauth\/callback&quot;)<\/p>\n<p>    #\u30ea\u30af\u30a8\u30b9\u30c8\u30c8\u30fc\u30af\u30f3\u3092\u30bb\u30c3\u30b7\u30e7\u30f3\u306b\u683c\u7d0d<br \/>\n    session[:request_token] = request_token<\/p>\n<p>    #Twitter\u8a8d\u8a3c\u753b\u9762\u3078\u30ea\u30c0\u30a4\u30ec\u30af\u30c8<br \/>\n    authorized_url = request_token.authorize_url<br \/>\n    redirect_to authorized_url<\/p>\n<p>  end<\/p>\n<p>  #&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n  # Twitter \u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u51e6\u7406<br \/>\n  #&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n  def callback<br \/>\n    #\u30ea\u30af\u30a8\u30b9\u30c8\u30c8\u30fc\u30af\u30f3\u53d6\u5f97<br \/>\n    request_token = session[:request_token]<\/p>\n<p>    #HTTP\u30d1\u30e9\u30e1\u30fc\u30bf\u53d6\u5f97<br \/>\n    oauth_verifier = params[:oauth_verifier]<\/p>\n<p>    #\u30a2\u30af\u30bb\u30b9\u30c8\u30fc\u30af\u30f3\u53d6\u5f97<br \/>\n    access_token = request_token.get_access_token(:oauth_verifier =&gt; oauth_verifier)<\/p>\n<p>    #\u30c8\u30fc\u30af\u30f3\u3068\u30c8\u30fc\u30af\u30f3\u30b7\u30fc\u30af\u30ec\u30c3\u30c8\u53d6\u5f97<br \/>\n    token = access_token.token<br \/>\n    token_secret = access_token.secret<\/p>\n<p>    #\u30a2\u30af\u30bb\u30b9\u30c8\u30fc\u30af\u30f3\u3092\u30bb\u30c3\u30b7\u30e7\u30f3\u306b\u683c\u7d0d<br \/>\n    session[:access_token] = access_token<\/p>\n<p>    #Twitter\u6295\u7a3f\u51e6\u7406<br \/>\n    twitter = Twitter::Client.new(:oauth_access =&gt; { :key =&gt; token, :secret =&gt; token_secret})<br \/>\n    twitter.status(:post, &quot;Twitter4R \u304b\u3089\u306eTweet\u3002\u6210\u529f\u3059\u308b\u304b\u306a?&quot;)<br \/>\n  end<\/p>\n<p>end<br \/>\n[\/ruby]<\/p>\n<p>\u30b3\u30c4\u306f\u3001OAuth\u8a8d\u8a3c\u306e\u4ed5\u7d44\u307f\u3002\u30b3\u30f3\u30b7\u30e5\u30fc\u30de\u30ad\u30fc\u3068\u30b3\u30f3\u30b7\u30e5\u30fc\u30de\u30b7\u30fc\u30af\u30ec\u30c3\u30c8\u3002\u30ea\u30af\u30a8\u30b9\u30c8\u30c8\u30fc\u30af\u30f3\u3084\u30a2\u30af\u30bb\u30b9\u30c8\u30fc\u30af\u30f3\u306e\u6982\u5ff5\u3068\u5f79\u5272\u3092\u3057\u3063\u304b\u308a\u3068\u7406\u89e3\u3059\u308b\u3053\u3068\u3002<\/p>\n<p>\u4e00\u756a\u30cf\u30de\u30c3\u305f\u306e\u306f\u3001Twitter\u958b\u767a\u8005\u306e\u30a2\u30d7\u30ea\u7ba1\u7406\u753b\u9762\u3067\u3001CallBack URL \u3092\u8a2d\u5b9a\u3057\u3066\u3044\u308b\u306e\u306b\u3001PIN\u30b3\u30fc\u30c9\u306e\u753b\u9762\u306b\u9077\u79fb\u3057\u3066\u3057\u307e\u3046\u73fe\u8c61\u3002<br \/>\nconsumer.get_request_token \u306e\u3068\u3053\u308d\u3067\u3001callback_url \u3092\u8a2d\u5b9a\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3002\u306a\u305c\u3060\u304b\u306f\u4e0d\u660e\u3002OAuth\u8a8d\u8a3c\u306e\u4ed5\u69d8\u306a\u306e\u304b\u3001Ruby for OAuth\u306e\u4e0d\u5099\u306a\u306e\u304b\uff1f<\/p>\n<p>\u30ea\u30af\u30a8\u30b9\u30c8\u30c8\u30fc\u30af\u30f3\u304b\u3089\u30a2\u30af\u30bb\u30b9\u30c8\u30fc\u30af\u30f3\u3092\u53d6\u5f97\u3059\u308b\u90e8\u5206\u3084\u3001Twitter::Client.new \u306e\u65b9\u6cd5\u3002\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u304c\u5c11\u306a\u3044\u30fb\u30fb\u30fb\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u56db\u82e6\u516b\u82e6\u3057\u3066\u3001\u3088\u3046\u3084\u304f\u8a8d\u8a3c\u51e6\u7406\u3068\u30c4\u30a4\u30fc\u30c8\u307e\u3067\u305f\u3069\u308a\u7740\u3044\u305f\u3002 [ruby] #coding: utf-8 require &#8216;twitter&#8217; require &#8216;oauth&#8217;  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"saved_in_kubio":false,"footnotes":""},"categories":[14,16,4],"tags":[],"class_list":["post-394","post","type-post","status-publish","format-standard","hentry","category-ruby","category-twitter","category-tech"],"_links":{"self":[{"href":"https:\/\/www.blowfish.co.jp\/index.php?rest_route=\/wp\/v2\/posts\/394","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.blowfish.co.jp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.blowfish.co.jp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.blowfish.co.jp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.blowfish.co.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=394"}],"version-history":[{"count":0,"href":"https:\/\/www.blowfish.co.jp\/index.php?rest_route=\/wp\/v2\/posts\/394\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.blowfish.co.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=394"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.blowfish.co.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=394"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.blowfish.co.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}