没有路线匹配 [GET] "/auth/twitter" OmniA
No route matches [GET] "/auth/twitter" OmniA
我没有使用设计或其他类似的东西-gem。我对 RoR 很陌生。
这是我的 routes.rb
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
Rails.application.routes.draw do
get "about", to: "about#index"
get "password", to: "passwords#edit", as: :edit_password
patch "password", to: "passwords#update"
get "password/reset", to: "password_resets#new"
post "password/reset", to: "password_resets#create"
get "password/reset/edit", to: "password_resets#edit"
patch "password/reset/edit", to: "password_resets#update"
get '/auth/:provider/callback', to: 'sessions#create'
get "sign_up", to: "registrations#new"
post "sign_up", to: "registrations#create"
get "sign_in", to: "sessions#new"
post "sign_in", to: "sessions#create"
delete "logout", to: "sessions#destroy"
root to: "main#index"
end
这里是user.rb
# email:string
# password_digest:string
#
# password:string virtual
# password_confirmation:string virtual
class User < ApplicationRecord
has_secure_password
validates :email, presence: true, format: { with: /\A[^@\s]+@[^@\s]+\z/, message: "must be a valid email address" }
end
这是我的 omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
provider :twitter,Rails.application.credentials.dig(:twitter,:api_key), Rails.application.credentials.dig(:twitter,:api_key)
end
我已经在我的 Twitter 应用程序中进行了所有设置。请帮忙。
似乎对 OmniAuth gem 进行了更改,默认情况下它仅允许 POST 请求。将此添加到您的 omniauth.rb
文件以允许 GET 请求:
OmniAuth.config.allowed_request_methods = [:post, :get]
进行此更改是出于安全原因。不幸的是,我无法解释。但这将使您完成 GoRails 教程!
此时,Twitter API 开始推出其 API 的 v2 版本。 tutorial makes use of v1.1 所以请确保同时使用那个。为此,请创建一个独立应用程序:
将 API 密钥添加到 Rails 凭证并将 http://localhost:3000/auth/twitter/callback
添加到 Callbacks URL 配置后Twitter 开发人员门户,将以下额外行添加到 omniauth.rb
,这些行将重新启用对 localhost:3000/auth/twitter
的获取请求并从控制台删除安全警告:
# Required to allow get requests, which enables a security flaw but that's how the tutorial is set up.
OmniAuth.config.allowed_request_methods = [:post, :get]
OmniAuth.config.silence_get_warning = true
Rails.application.config.middleware.use OmniAuth::Builder do
provider :twitter, Rails.application.credentials.dig(:twitter, :api_key), Rails.application.credentials.dig(:twitter, :api_secret)
end
重新启动您的 Rails 服务器并导航到 localhost:3000/auth/twitter
,这应该会将您重定向到看起来类似于以下内容的 url:
https://api.twitter.com/oauth/authenticate?oauth_token=xov0NQAAAAABMcOqAAABd3F1_T0
如果正在使用的 Twitter 帐户尚未启用该应用程序,此 URL 将呈现授权屏幕:
前提是你的OmniauthCallbacksController
长这样:
class OmniauthCallbacksController < ApplicationController
def twitter
render plain: "success"
end
end
您的应用应该重定向到 /auth/twitter/callback
。 url 看起来像 http://localhost:3000/auth/twitter/callback?oauth_token=D4V2tAAAAAABMcOqAAABd3GFmgM&oauth_verifier=TSxCgaVsoQzY039l5DKQBJQiLKkaWBCA
,它应该在页面上打印 success
。
希望对您有所帮助!
我是 Ruby 的 Rails 初学者课程的作者。我已经更新了视频以反映这些变化。
Omniauth 2.0 已发布,要求您现在使用 POST 请求以确保安全。
现在我们将添加两个宝石:
bundle add omniauth-twitter omniauth-rails_csrf_protection
并确保您已将 api_secret
作为 omniauth.rb 初始值设定项中的第二个参数:
Rails.application.config.middleware.use OmniAuth::Builder do
provider :twitter,Rails.application.credentials.dig(:twitter,:api_key), Rails.application.credentials.dig(:twitter,:api_secret)
end
然后您可以通过将 method: :post
添加到您的 link_to
或 button_to
来重定向到 Twitter
link_to "Connect Twitter", "/auth/twitter", method: :post, class: "btn btn-primary"
button_to "Connect Twitter", "/auth/twitter", method: :post, class: "btn btn-primary"
这适用于项目和独立 Twitter 应用程序,因此您可以使用任何一个。
我没有使用设计或其他类似的东西-gem。我对 RoR 很陌生。
这是我的 routes.rb
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
Rails.application.routes.draw do
get "about", to: "about#index"
get "password", to: "passwords#edit", as: :edit_password
patch "password", to: "passwords#update"
get "password/reset", to: "password_resets#new"
post "password/reset", to: "password_resets#create"
get "password/reset/edit", to: "password_resets#edit"
patch "password/reset/edit", to: "password_resets#update"
get '/auth/:provider/callback', to: 'sessions#create'
get "sign_up", to: "registrations#new"
post "sign_up", to: "registrations#create"
get "sign_in", to: "sessions#new"
post "sign_in", to: "sessions#create"
delete "logout", to: "sessions#destroy"
root to: "main#index"
end
这里是user.rb
# email:string
# password_digest:string
#
# password:string virtual
# password_confirmation:string virtual
class User < ApplicationRecord
has_secure_password
validates :email, presence: true, format: { with: /\A[^@\s]+@[^@\s]+\z/, message: "must be a valid email address" }
end
这是我的 omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
provider :twitter,Rails.application.credentials.dig(:twitter,:api_key), Rails.application.credentials.dig(:twitter,:api_key)
end
我已经在我的 Twitter 应用程序中进行了所有设置。请帮忙。
似乎对 OmniAuth gem 进行了更改,默认情况下它仅允许 POST 请求。将此添加到您的 omniauth.rb
文件以允许 GET 请求:
OmniAuth.config.allowed_request_methods = [:post, :get]
进行此更改是出于安全原因。不幸的是,我无法解释。但这将使您完成 GoRails 教程!
此时,Twitter API 开始推出其 API 的 v2 版本。 tutorial makes use of v1.1 所以请确保同时使用那个。为此,请创建一个独立应用程序:
将 API 密钥添加到 Rails 凭证并将 http://localhost:3000/auth/twitter/callback
添加到 Callbacks URL 配置后Twitter 开发人员门户,将以下额外行添加到 omniauth.rb
,这些行将重新启用对 localhost:3000/auth/twitter
的获取请求并从控制台删除安全警告:
# Required to allow get requests, which enables a security flaw but that's how the tutorial is set up.
OmniAuth.config.allowed_request_methods = [:post, :get]
OmniAuth.config.silence_get_warning = true
Rails.application.config.middleware.use OmniAuth::Builder do
provider :twitter, Rails.application.credentials.dig(:twitter, :api_key), Rails.application.credentials.dig(:twitter, :api_secret)
end
重新启动您的 Rails 服务器并导航到 localhost:3000/auth/twitter
,这应该会将您重定向到看起来类似于以下内容的 url:
https://api.twitter.com/oauth/authenticate?oauth_token=xov0NQAAAAABMcOqAAABd3F1_T0
如果正在使用的 Twitter 帐户尚未启用该应用程序,此 URL 将呈现授权屏幕:
前提是你的OmniauthCallbacksController
长这样:
class OmniauthCallbacksController < ApplicationController
def twitter
render plain: "success"
end
end
您的应用应该重定向到 /auth/twitter/callback
。 url 看起来像 http://localhost:3000/auth/twitter/callback?oauth_token=D4V2tAAAAAABMcOqAAABd3GFmgM&oauth_verifier=TSxCgaVsoQzY039l5DKQBJQiLKkaWBCA
,它应该在页面上打印 success
。
希望对您有所帮助!
我是 Ruby 的 Rails 初学者课程的作者。我已经更新了视频以反映这些变化。
Omniauth 2.0 已发布,要求您现在使用 POST 请求以确保安全。
现在我们将添加两个宝石:
bundle add omniauth-twitter omniauth-rails_csrf_protection
并确保您已将 api_secret
作为 omniauth.rb 初始值设定项中的第二个参数:
Rails.application.config.middleware.use OmniAuth::Builder do
provider :twitter,Rails.application.credentials.dig(:twitter,:api_key), Rails.application.credentials.dig(:twitter,:api_secret)
end
然后您可以通过将 method: :post
添加到您的 link_to
或 button_to
link_to "Connect Twitter", "/auth/twitter", method: :post, class: "btn btn-primary"
button_to "Connect Twitter", "/auth/twitter", method: :post, class: "btn btn-primary"
这适用于项目和独立 Twitter 应用程序,因此您可以使用任何一个。