質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

499閲覧

SorceryでのTwitter連携ができません。

lilywhite

総合スコア42

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2019/01/15 08:52

SorceryというGemを使ってTwitterと連携・登録したいのですが下記のようなエラーが発生します。下記のコードに問題があるそうです。

OAuth::Unauthorized in OauthsController#oauth

401 Authorization Required

ruby

1# and after authorizing there back to the callback url. 2def oauth 3 login_at(params[:provider]) 4end 5 6def callback

こちらが上記のエラー発生したファイルの全部です。

Ruby

1class OauthsController < ApplicationController 2 skip_before_action :require_login, raise: false 3 4 # sends the user on a trip to the provider, 5 # and after authorizing there back to the callback url. 6 def oauth 7 login_at(params[:provider]) 8 end 9 10 def callback 11 provider = params[:provider] 12 if @user = login_from(provider) 13 redirect_to root_path, :notice => "Logged in from #{provider.titleize}!" 14 else 15 begin 16 @user = create_from(provider) 17 # NOTE: this is the place to add '@user.activate!' if you are using user_activation submodule 18 19 reset_session # protect from session fixation attack 20 auto_login(@user) 21 redirect_to root_path, :notice => "Logged in from #{provider.titleize}!" 22 rescue 23 redirect_to root_path, :alert => "Failed to login from #{provider.titleize}!" 24 end 25 end 26 end 27 28 #example for Rails 4: add private method below and use "auth_params[:provider]" in place of 29 #"params[:provider] above. 30 31 # private 32 # def auth_params 33 # params.permit(:code, :provider) 34 # end 35 36end

こちらがsorceryの設定ファイルです。

Ruby

1... 2Rails.application.config.sorcery.submodules = [:external] 3 4# Here you can configure each submodule's features. 5Rails.application.config.sorcery.configure do |config| 6 7 ... 8 9 config.external_providers = [:twitter] 10 11 ... 12 13 # Twitter will not accept any requests nor redirect uri containing localhost, 14 # make sure you use 0.0.0.0:3000 to access your app in development 15 # 16 config.twitter.key = "1B3rI5LuFGz7Niwo0D1hU3AlW" 17 config.twitter.secret = "kUcT9xagNqgFzh9lbWP08zNM5QbcQ3agRj0SHXVHd7diya8Rpq" 18 config.twitter.callback_url = "http://192.168.33.15:3000/oauth/callback?provider=twitter" 19 config.twitter.user_info_mapping = {:email => "screen_name"} 20 21 ... 22 23 # --- user config --- 24 config.user_config do |user| 25 26 ... 27 28 # -- external -- 29 # Class which holds the various external provider data for this user. 30 # Default: `nil` 31 # 32 user.authentications_class = Authentication 33 34 # User's identifier in authentications class. 35 # Default: `:user_id` 36 # 37 # user.authentications_user_id_attribute_name = 38 39 # Provider's identifier in authentications class. 40 # Default: `:provider` 41 # 42 # user.provider_attribute_name = 43 44 # User's external unique identifier in authentications class. 45 # Default: `:uid` 46 # 47 # user.provider_uid_attribute_name = 48 end 49 50 # This line must come after the 'user config' block. 51 # Define which model authenticates with sorcery. 52 config.user_class = 'User' 53end 54

Twitterデベロッパーツールの設定にはCALLBACK_URLも追加してあります。
APIキーも間違っていませんでした。

ふぉなたか原因わかる方いらしましたら教えてください。お願いします

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

いじくり回しているうちに訳がわからなくなったのでイチからやり直しました

投稿2019/01/16 04:11

lilywhite

総合スコア42

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問