Last active
April 2, 2024 13:22
-
-
Save seratch/08bddda24be01f242c2a to your computer and use it in GitHub Desktop.
Revisions
-
seratch revised this gist
Sep 25, 2014 . 2 changed files with 5 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,7 +4,7 @@ https://apps.twitter.com/ API Keys のタブから「Application settings」を参照して使います。 https://apps.twitter.com/app/***/keys @@ -15,6 +15,8 @@ https://apps.twitter.com/app/***/keys post させるアプリをつくるなら「Access level」が「Read and write」であることを確認してください。 注意点としては「Settings」の「Callback URL」が未設定だとエラーになります。 ### OmniAuth OAuth 1.0a, 2.0, OpenID など色々ある認証・認可のプロトコルの差異を吸収して、扱いやすい認証・認可の仕組みをプラグイン機構として提供している gem。 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -11,8 +11,8 @@ <hr/> <h2>Home Timeline</h2> <% @timeline.each do |tweet| %> <!--<pre><%= JSON.pretty_generate(tweet.attrs) %></pre>--> <p><%= tweet.text %> by @<%= tweet.user.screen_name %></p> <% end %> </body> </html> -
seratch revised this gist
Sep 25, 2014 . 1 changed file with 4 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -27,5 +27,8 @@ Rails でよく利用されるが Rails だけでなく Sinatra でも利用可 http://recipes.sinatrarb.com/p/middleware/twitter_authentication_with_omniauth ### アプリの起動 ``` bundle exec ruby app.rb ``` -
seratch revised this gist
Sep 25, 2014 . 1 changed file with 1 addition and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -34,7 +34,7 @@ def twitter end before do pass if request.path_info =~ /^\/auth\// redirect to('/auth/twitter') unless logged_in? end @@ -47,10 +47,6 @@ def twitter end get '/' do @oauth = session[:twitter_oauth] @timeline = twitter.home_timeline erb :index -
seratch created this gist
Sep 25, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,8 @@ source 'https://rubygems.org/' gem 'sinatra' gem 'omniauth-twitter' gem 'json' gem 'twitter' gem 'dotenv' gem 'thin' This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,62 @@ require 'sinatra' require 'omniauth-twitter' require 'twitter' require 'json' # load ./.env file require 'dotenv' Dotenv.load puts "ENV['TWITTER_CONSUMER_KEY']: #{ENV['TWITTER_CONSUMER_KEY']}" set :server, :thin set :session_secret, DateTime.now.to_s configure do enable :sessions use OmniAuth::Builder do provider :twitter, ENV['TWITTER_CONSUMER_KEY'], ENV['TWITTER_CONSUMER_SECRET'] end end helpers do def logged_in? session[:twitter_oauth] end def twitter Twitter::REST::Client.new do |config| config.consumer_key = ENV['TWITTER_CONSUMER_KEY'] config.consumer_secret = ENV['TWITTER_CONSUMER_SECRET'] config.access_token = session[:twitter_oauth][:token] config.access_token_secret = session[:twitter_oauth][:secret] end end end before do pass if request.path_info =~ /^\/auth\// || request.path_info == '/' redirect to('/auth/twitter') unless logged_in? end get '/auth/twitter/callback' do session[:twitter_oauth] = env['omniauth.auth'][:credentials] redirect to('/') end get '/auth/failure' do end get '/' do erb :top end get '/timeline' do @oauth = session[:twitter_oauth] @timeline = twitter.home_timeline erb :index end get '/logout' do session.clear redirect to('/') end This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,31 @@ ## Twitter 認証付き Sinatra アプリをつくろう ### Twitter App を用意 https://apps.twitter.com/ API Keys のタブから「Application settings」を参照して使います https://apps.twitter.com/app/***/keys - API key - API secret が必要な項目です。 post させるアプリをつくるなら「Access level」が「Read and write」であることを確認してください。 ### OmniAuth OAuth 1.0a, 2.0, OpenID など色々ある認証・認可のプロトコルの差異を吸収して、扱いやすい認証・認可の仕組みをプラグイン機構として提供している gem。 https://github.com/intridea/omniauth Rails でよく利用されるが Rails だけでなく Sinatra でも利用可能。 ### Sinatra app with OmniAuth http://recipes.sinatrarb.com/p/middleware/twitter_authentication_with_omniauth まだ途中.. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,18 @@ <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> </head> <body> <a href="/logout">Logout</a><br/> <hr/> <h2>Access Token</h2> <pre><%= JSON.pretty_generate(@oauth) %></pre> <hr/> <h2>Home Timeline</h2> <% @timeline.each do |tweet| %> <!--<pre><%= JSON.pretty_generate(tweet.attrs) %></pre>--> <p><%= tweet.text %> by @<%= tweet.user.screen_name %></p> <% end %> </body> </html>