【问题标题】:google oauth fails with 401 unauthorized in rails omniauth谷歌 oauth 失败,在 rails omniauth 中出现 401 未授权
【发布时间】:2013-04-15 20:43:04
【问题描述】:

我正在尝试使用设计和omniauth 向google oauth2 进行身份验证。我按照omniauth文档中的规定虔诚地设置了回调,甚至使用了相同的确切代码。 https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview

我把代码上传到了github:https://github.com/kiranjulapalli/devisetest

我被重定向到登录页面,当我选择我的帐户时,我再次被重定向到登录页面。我在这里错过了什么?

这是日志:

Started GET "/users/auth/google_oauth2/callback?state=3baa394efe9c586b30fgfc05b1f618af04b7728d0d75c3c1&code=4/HxV9O
37jKYgxg10EWDntdfewZTz3.4kqevOMEweMTshQV0ieZDArQUdYefAI" for 127.0.0.1 at 2013-04-15 16:36:01 -0400
Processing by Users::OmniauthCallbacksController#google_oauth2 as HTML
  Parameters: {"state"=>"3baa394efe9c586b30fgfc05b1f618af04b7728d0d75c3c1", "code"=>"4/HxV9O
37jKYgxg10EWDntdfewZTz3.4kqevOMEweMTshQV0ieZDArQUdYefAI"}
  MOPED: 127.0.0.1:27017 COMMAND      database=admin command={:ismaster=>1} (2.0001ms)
  MOPED: 127.0.0.1:27017 QUERY        database=devisetest_development collection=users selector={"$query"=>{"email"
=>"myemailaddress@gmail.com"}, "$orderby"=>{:_id=>1}} flags=[:slave_ok] limit=-1 skip=0 batch_size=nil fields=nil
(0.9999ms)
Completed 401 Unauthorized in 24ms
#custom log statements
logger::Find if this user exists
logger::Return user
logger:Got call back
logger:User persisted

我的 routes.rb 文件:

Devisetest::Application.routes.draw do
  devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
  resources :users
  root :to => 'home#index'
end

【问题讨论】:

    标签: ruby-on-rails devise ruby-on-rails-3.2 omniauth google-oauth


    【解决方案1】:

    您要求您的用户在登录前通过电子邮件确认。 (从 :confirmable 参数到您的 user.rb 中设计)

    您可以通过 rails 控制台删除 :confirmable 或手动为 user.confirmed_at 分配一个值(例如 Time.now)。

    您还应该在布局中的某处显示 flash[:notice],您会看到: “已成功通过 Google 帐户进行身份验证。您必须先确认您的帐户,然后才能继续。”

    看起来像: Always getting 401 Unauthorized with new install of Rails + Devise 是一个类似的问题。

    【讨论】:

    • 谢谢,我在 application.html.erb 中有这个 <% flash.each do |name, msg| %> <% content_tag :div, msg, id: "flash_#{name}" %> <% end %>。它不会打印 flash 通知和警报吗?
    • 如果你这样做了:<%= content_tag :div, msg, id: "flash_#{name}" %> 你忘记了 =。
    • 啊,这解释了......谢谢。我正在尝试设计 skip_confirmable!
    • 这就是问题所在。谢谢。现在我需要回去弄清楚在哪里放置skip_confirmable..
    • models/user.rb find_for_google_oauth2,您需要将 create 更改为 new,然后执行 user.skip_confirmation!然后 user.save
    猜你喜欢
    • 2012-02-13
    • 1970-01-01
    • 2014-11-10
    • 2012-06-24
    • 1970-01-01
    • 2021-06-23
    • 1970-01-01
    • 1970-01-01
    • 2013-06-15
    相关资源
    最近更新 更多