OAuth::Unauthorized 401 Unauthorized for omniauth-twitter in rails
Posted
技术标签:
【中文标题】OAuth::Unauthorized 401 Unauthorized for omniauth-twitter in rails【英文标题】: 【发布时间】:2013-02-22 08:21:08 【问题描述】:当我点击 [http://127.0.0.1:3000/auth/twitter] 时,我得到 OAuth::Unauthorized 401 导轨中出现未经授权的错误。我正在关注 Railscast 视频 #241,以使用我的 rails 应用程序进行 Twitter 身份验证。我用谷歌搜索了很多,但找不到答案。
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
【问题讨论】:
您是否在 twitter.com 检查过您的应用程序设置?确保您有任何 URL 作为回调。 【参考方案1】:omniauth.rb
是什么ENV['75UOAIDmKrRXvXKBhNvKA'], ENV['GrIaBI0tQy2TtjOtaFL9VxT6s9qq1sV7h9yRaZW4A']
?
如果75UOAIDmKrRXvXKBhNvKA
和GrIaBI0tQy2TtjOtaFL9VxT6s9qq1sV7h9yRaZW4A
是你的APP_ID
和APP_SECRET
,那么它应该写成:
provider :twitter, '75UOAIDmKrRXvXKBhNvKA', 'GrIaBI0tQy2TtjOtaFL9VxT6s9qq1sV7h9yRaZW4A'
【讨论】:
@varunvlalan,我厌倦了你的建议,但得到同样的错误。不知道该怎么办? 我在 routes.rb 中注意到的另一件事match 'auth/twitter/callback', to: 'sessions#create'
在路径前加上“/”。 match '/auth/twitter/callback', to: 'sessions#create'
@VSiingh 出了什么问题?【参考方案2】:
我遇到了同样的问题,解决我的问题的是:
变化:
ENV['75UOAIDmKrRXvXKBhNvKA'],ENV['GrIaBI0tQy2TtjOtaFL9VxT6s9qq1sV7h9yRaZW4A']
到:
'75UOAIDmKrRXvXKBhNvKA', 'GrIaBI0tQy2TtjOtaFL9VxT6s9qq1sV7h9yRaZW4A'
并在此处的“auth”之前放置一个“/”:
match '/auth/:provider/callback', to: 'sessions#create', via: 'get'
match '/auth/failure', to: redirect('/'), via: 'get'
【讨论】:
以上是关于OAuth::Unauthorized 401 Unauthorized for omniauth-twitter in rails的主要内容,如果未能解决你的问题,请参考以下文章
带有 Rails 5 的 Omniauth-twitter 停止工作! OAuth::Unauthorized 403 Forbidden
错误:[polling_error] "code":"ETELEGRAM","message":"ETELEGRAM: 401 U