Reputation: 867
When I click on [http://127.0.0.1:3000/auth/twitter], I am getting OAuth::Unauthorized 401 Unauthorized error in rails. I am following Railscast video #241 for Twitter authentication with my rails application. I have googled a lot for it but could not find answer for it.
Info regarding app on twitter:
Callback URL: [http://127.0.0.1:3000/auth/twitter/callback]
Website: [http://127.0.0.1:3000]
### omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
# provider :developer unless Rails.env.production?
provider :twitter, ENV['75UOAIDmKrRXvXKBhNvKA'], ENV['GrIaBI0tQy2TtjOtaFL9VxT6s9qq1sV7h9yRaZW4A']
end
### routes.rb
Chilli::Application.routes.draw do
resources :posts
root :to => 'posts#index'
#match '/auth/:twitter/callback' => 'sessions#create', :as => :auth_callback
match 'auth/twitter/callback', to: 'sessions#create'
end
### application.html.erb
<div id="user_nav">
<%= link_to "Sign in with Twitter", "/auth/twitter"%>
</div>
### sessions_controller.rb
class SessionsController < ApplicationController
def create
user = User.from_omniauth(env['omniauth.auth'])
session[:user_id] = user.id
redirect_to root_url, notice: "Signed in."
end
end
### user.rb
class User < ActiveRecord::Base
attr_accessible :name, :provider, :uid
def self.from_omniauth(auth)
where(auth.slice("provider", "uid")).first || create_from_omniauth(auth)
end
def self.create_from_omniauth(auth)
create! do |user|
user.provider = auth["provider"]
user.uid = auth["uid"]
user.name = auth["info"]["nickname"]
end
end
end
Upvotes: 5
Views: 6320
Reputation: 135
I had the same issue, what fixed my issue was:
Changing:
ENV['75UOAIDmKrRXvXKBhNvKA'],ENV['GrIaBI0tQy2TtjOtaFL9VxT6s9qq1sV7h9yRaZW4A']
to:
'75UOAIDmKrRXvXKBhNvKA', 'GrIaBI0tQy2TtjOtaFL9VxT6s9qq1sV7h9yRaZW4A'
and putting a '/' before 'auth' here:
match '/auth/:provider/callback', to: 'sessions#create', via: 'get'
match '/auth/failure', to: redirect('/'), via: 'get'
Upvotes: 1
Reputation: 950
In omniauth.rb
what is
ENV['75UOAIDmKrRXvXKBhNvKA'], ENV['GrIaBI0tQy2TtjOtaFL9VxT6s9qq1sV7h9yRaZW4A']
?
If 75UOAIDmKrRXvXKBhNvKA
and GrIaBI0tQy2TtjOtaFL9VxT6s9qq1sV7h9yRaZW4A
are your APP_ID
and APP_SECRET
then it should be written as:
provider :twitter, '75UOAIDmKrRXvXKBhNvKA', 'GrIaBI0tQy2TtjOtaFL9VxT6s9qq1sV7h9yRaZW4A'
Upvotes: 12