14
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

omniauth-twitterでTwitterのアクセストークンを取得する

Posted at
  • Rails 4.2.4で動作確認
  • アクセストークン取得するだけ
    • device使わない
    • modelも作らない
  • Twitterのアプリケーション登録は適宜ぐぐって

omniauth-twitterを入れる

Gemfile
gem 'omniauth-twitter'
$ bundle install

API_KEYなどの設定

アプリケーション登録する

適宜ぐぐる。

callback urlは、開発時はhttp://localhost:3000/auth/twitter/callbackで登録する

設定書く

config/initializers/omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
  provider :twitter, ENV['TWITTER_API_KEY'], ENV['TWITTER_API_SECRET']
end

ここまで書いてRailsサーバー立ち上げてhttp://localhost:3000/auth/twitterにアクセスするとTwitterのアプリ認証画面に飛ぶ

Callback設定する

登録したcallback urlに対応するActionを実装する

config/routes.rb
get 'auth/twitter/callback', to: 'twitter_authentication#callback' 
app/controllers/twitter_authentication_controller.rb
class TwitterAuthenticationController < ApplicationController
  def callback
    omniauth = request.env['omniauth.auth']
    omniauth.credentials.token
    omniauth.credentials.secret
  end
end

その他のparameterはAuth Hash Schema · intridea/omniauth Wikiを見る

終わり

14
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?