尝试在 rails 中使用 Gmail API 进行身份验证时,没有路由匹配 [GET] "/auth/google_oauth2" 错误

Joh*_*ohn 2 routes ruby-on-rails omniauth gmail-api

尝试使用 Rails 中的 Gmail API 进行身份验证时,出现 No route matching [GET] "/auth/google_oauth2" 错误。

按照这篇文章https://www.twilio.com/blog/2014/09/gmail-api-oauth-rails.html,我正在实现 Gmail API 集成。

代码似乎是正确的。但不知道是什么问题。

在路由文件中:

  root to: 'visitors#index'
  if defined?(Devise)
    devise_for :users, :controllers => { :registrations => "registrations", :passwords => "passwords", omniauth_callbacks: 'omniauth_callbacks' }
    devise_scope :user do
      get 'auth/:provider/callback', :to => 'users/omniauth_callbacks#facebook'
    end
  end
  get "/auth/:provider/callback" => "candidates#authenticate_from_google"
Run Code Online (Sandbox Code Playgroud)

在 rake 路线中,我什至也有路线。

GET /auth/:provider/callback(.:format)  candidates#authenticate_from_google

  
Run Code Online (Sandbox Code Playgroud)

在 Gemfile 中:-

gem "omniauth"
gem "omniauth-linkedin"
gem "jwt", "~> 1.4.1"
gem "linkedin-scraper", "~> 0.1.5"
gem "omniauth-facebook"
gem "omniauth-google-oauth2"
gem 'signet' 
gem 'google-api-client', '0.8.2'
Run Code Online (Sandbox Code Playgroud)

在视图中

   <%= link_to("Sync", "/auth/google_oauth2" , class: "btn btn-sm btn-primary", method: :get)%>
Run Code Online (Sandbox Code Playgroud)

在/app/controllers/candidates_controller.rb

   def authenticate_from_google
    @auth = request.env['omniauth.auth']['credentials']
    if @auth
      @identity = current_user.identities.new
      @identity.provider = "google_oauth2"
      @identity.access_token = @auth['token']
      @identity.refresh_token = @auth['refresh_token']
      @identity.expires_at = Time.at(@auth['expires_at']).to_datetime
      @identity.save
      flash[:notice] = "Successfully Authenticated from Google"
    else
      flash[:notice] = "Google Authentication failed"
    end
    redirect_to account_path
  end
Run Code Online (Sandbox Code Playgroud)

在 /config/initializers/omniauth.rb

  Rails.application.config.middleware.use OmniAuth::Builder do
  provider :google_oauth2, ENV['GOOGLE_API_KEY'], ENV['GOOGLE_SECRET_KEY'], {
  scope: ['email',
    'https://www.googleapis.com/auth/gmail.send'],
    access_type: 'offline'}
end
Run Code Online (Sandbox Code Playgroud)

在 /config/initializers/devise.rb

Devise.setup do |config|

  config.omniauth :linkedin, ENV['LINKEDIN_API_KEY'], ENV['LINKEDIN_SECRET_KEY']
  config.omniauth :facebook, ENV['FACEBOOK_API_KEY'], ENV['FACEBOOK_SECRET_KEY'] ,
              :info_fields => 'email,name,first_name,last_name,verified', :display => 'page', :scope => 'email'

   config.omniauth :google_oauth2, ENV['GOOGLE_API_KEY'],
                               ENV['GOOGLE_SECRET_KEY'],
                               scope: 'email,https://www.googleapis.com/auth/gmail.send',
                               access_type: 'offline'#, :prompt => "select_account",
                                skip_jwt: true
    config.timeout_in = 7.days
end
Run Code Online (Sandbox Code Playgroud)

小智 8

对于 omniauth gem >= 2.0.0,您必须指定您将允许在链接按钮中使用的请求方法。

<%= link_to("Sync", "/auth/google_oauth2" , class: "btn btn-sm btn-primary", method: :get)%>

只需添加一行OmniAuth.config.allowed_request_methods = [:post, :get]

# config/initializers/omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
  OmniAuth.config.allowed_request_methods = [:post, :get]
  provider :google_oauth2, ENV['GOOGLE_API_KEY'], ENV['GOOGLE_SECRET_KEY'], {
  scope: ['email',
    'https://www.googleapis.com/auth/gmail.send'],
    access_type: 'offline'}
end
Run Code Online (Sandbox Code Playgroud)

  • 这帮助我解决了我的问题,谢谢! (2认同)