Skip to content

Instantly share code, notes, and snippets.

@seratch
Last active April 2, 2024 13:22
Show Gist options
  • Select an option

  • Save seratch/08bddda24be01f242c2a to your computer and use it in GitHub Desktop.

Select an option

Save seratch/08bddda24be01f242c2a to your computer and use it in GitHub Desktop.

Revisions

  1. seratch revised this gist Sep 25, 2014. 2 changed files with 5 additions and 3 deletions.
    4 changes: 3 additions & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@

    https://apps.twitter.com/

    API Keys のタブから「Application settings」を参照して使います
    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。
    4 changes: 2 additions & 2 deletions views_index.erb
    Original 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>
    <!--<pre><%= JSON.pretty_generate(tweet.attrs) %></pre>-->
    <p><%= tweet.text %> by @<%= tweet.user.screen_name %></p>
    <% end %>
    </body>
    </html>
  2. seratch revised this gist Sep 25, 2014. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion readme.md
    Original 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
    ```
  3. seratch revised this gist Sep 25, 2014. 1 changed file with 1 addition and 5 deletions.
    6 changes: 1 addition & 5 deletions app.rb
    Original file line number Diff line number Diff line change
    @@ -34,7 +34,7 @@ def twitter
    end

    before do
    pass if request.path_info =~ /^\/auth\// || request.path_info == '/'
    pass if request.path_info =~ /^\/auth\//
    redirect to('/auth/twitter') unless logged_in?
    end

    @@ -47,10 +47,6 @@ def twitter
    end

    get '/' do
    erb :top
    end

    get '/timeline' do
    @oauth = session[:twitter_oauth]
    @timeline = twitter.home_timeline
    erb :index
  4. seratch created this gist Sep 25, 2014.
    8 changes: 8 additions & 0 deletions Gemfile
    Original 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'
    62 changes: 62 additions & 0 deletions app.rb
    Original 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
    31 changes: 31 additions & 0 deletions readme.md
    Original 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

    まだ途中..

    18 changes: 18 additions & 0 deletions views_index.erb
    Original 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>