6
4

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.

Sorceryを使ったTwitterログイン

Last updated at Posted at 2018-04-16

1, Twitter Developerにアプリを登録!

必要になるのは、Consumer Key(API Key)とConsumer Secret(API Secret)です!

2, Sorceryの設定をする

https://github.com/Sorcery/sorcery/wiki/External
公式に従う。

sorcery.rb
Rails.application.config.sorcery.submodules = [:remember_me, :external]
  Rails.application.config.sorcery.configure do |config|
	config.external_providers = [:twitter]
	config.twitter.key = ENV["TWITTER_KEY"]
 	config.twitter.secret = ENV["TWITTER_SECRET"]
 	case Rails.env
 		when "production"
 			config.twitter.callback_url = ENV["TWITTER_CALLBACK"]
		when "development"
			config.twitter.callback_url = ENV["TWITTER_CALLBACK_DEVELOPMENT"]
	end
	config.twitter.user_info_mapping = { email: "screen_name" }
	
	user.authentications_class = Authentication
	
  end
end
	

3, ローカル環境で各keyのpathを通す!

export="あなたのAPI Key"
export="あなたのAPI Secret"
export="http://localhost:3000/oauth/callback?provider=twitter"
source ~/.bash_profile

pathが通っているかどうかexport -pで確認できます。

4, 注意点!!!

callbackURLの指定をしない場合、PINcode入力ありのログイン機能になってしまいます。
emailを取得してない、かつ
validates :email, uniqueness: true
をかけているとemail nullで引っかかります。

5, メールアドレスを取得するには?

スクリーンショット 2018-04-16 15.14.36.png

まずAdditional Permissionsを要求するためにプライバシーポリシー、利用規約ページのURLを設定します。
次に記述です。

sorcery.rb
  config.twitter.user_info_path = "/1.1/account/verify_credentials.json?include_email=true"
  config.twitter.user_info_mapping = { nickname: "screen_name", email: "email" }

これで終わりです!

6
4
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
6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?