Rails-在多个模型上使用omniauth时,Google oauth2 request.env ['omniauth.auth']为零

詹姆斯·N

我正在使用Rails应用程序,并使用omn​​iauth google oauth2 gem设置了Google OAuth2 有2个模型正在使用devise和omniauth,并且遵循本指南将devise与多个模型结合使用(来自devise团队)不起作用。

当前,存在1个现有模型,devise模型使用针对Facebook的omniauth策略。该模型的设置如下所示

devise :invitable, :database_authenticatable, :registerable,
         :trackable, :validatable, :omniauthable, omniauth_providers: [:facebook, :facebook_access_token]

在单独的模型上,我想为Google添加omniauth策略(该模型也在使用devise),该策略已设置了标准的设计

devise :database_authenticatable,
         :recoverable, :rememberable, :trackable, :validatable

目前在devise.rb这里为facebook omniauth设置了

config.omniauth :facebook, ENV['facebook_app_id'], ENV['facebook_app_secret'], { scope: 'comma,seperated,fields', info_fields: 'comma,seperated,fields' }

并在omniauth.rb那里设置了谷歌

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :google_oauth2, ENV['GOOGLE_CLIENT_ID'], ENV['GOOGLE_CLIENT_SECRET'], 
  {
    scope: 'userinfo.email, userinfo.profile',
    prompt: 'select_account',
    image_aspect_ratio: 'square',
    image_size: 50
  }
end

routes.rb有对色器件/ omn​​iauth各种途径

devise_for :users, path: 'users', controllers: {omniauth_callbacks: "users/omniauth_callbacks"} do
    get 'sign_out', to: 'devise/sessions#destroy', as: :destroy_user_session
  end

devise_for :admin_users, path: 'admin/admin_users'

注意-在controllers: {omniauth_callbacks: "admin/omniauth_callbacks"}route.rb中没有admin_users,因为在启动服务器时出现此错误

Please add `devise :omniauthable` to the `AdminUser` model

并且如果omniauthable同时添加到两个模型中,则在运行本地服务器时出现此错误

Wrong OmniAuth configuration. If you are getting this exception, it means that either:
1) You are manually setting OmniAuth.config.path_prefix and it doesn't match the Devise one
2) You are setting :omniauthable in more than one model
3) You changed your Devise routes/OmniAuth setting and haven't restarted your server

初始化文件和route.rb中的当前设置允许服务器打开。由于无法按照指南显示的方式使用此方法,因此我使用的是omniauth-google-oauth2 gem指南中概述1次混合身份验证流程在呈现该视图的页面中,我像这样设置了javascript

<script src="https://apis.google.com/js/platform.js?onload=init"></script>
<script type="text/javascript">
function init() {
  gapi.load('auth2', function() {
    // Ready.
    $('.google-login-button').click(function(e) {
      e.preventDefault();
      gapi.auth2.authorize({
        client_id: "SOMEKEY.apps.googleusercontent.com",
        cookie_policy: 'single_host_origin',
        scope: 'email profile',
        response_type: 'code'
      }, function(response) {
        if (response && !response.error) {
          // google authentication succeed, now post data to server.
          jQuery.ajax({type: 'POST', url: '/admin/admin-signin', data: response,
            success: function(data) {
              // response from server
              console.log('********************')
              console.log(data)
              console.log('********************')
            }
          });        
        } else {
          // google authentication failed
          console.log('********************')
          console.log(response)
          console.log('********************')
        }
      });
    });
  });
};
init();
</script>

<div class="authform">
  <a class="google-login-button">Sign in</a>
</div>

而且我已经添加了这样的路线

namespace :admin do 
  ...
  post '/admin-signin', to: 'application#google_oauth2'
end

目前,此设置可让我从Google呈现表单提示,我可以选择要使用的帐户。选择要使用的帐户后,我可以看到参数已发送到控制器操作,看起来像这样

{"code"=>"4/LONGCODEOFSTRINGSANDNUMBERS", 
 "scope"=>"email profile https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile openid", 
"authuser"=>"1", "hd"=>"COMPANY", "session_state"=>"b0c030e5f1304f58695e19aa29cb73c942ac69b6..163d", 
"prompt"=>"consent", "controller"=>"admin/application", "action"=>"google_oauth2"}

我处理此呼叫的控制器动作如下所示

def google_oauth2
  @admin_user = AdminUser.from_omniauth(request.env['omniauth.auth'])

  if @admin_user.persisted?
    flash[:notice] = I18n.t 'devise.omniauth_callbacks.success', kind: 'Google'
    sign_in_and_redirect @admin_user, event: :authentication
  else
    session['devise.google_data'] = request.env['omniauth.auth'].except(:extra) 
    redirect_to :back, alert: @admin_user.errors.full_messages.join("\n")
  end
end

但是问题是request.env['omniauth.auth']零,并且没有数据与之一起发送。看来我可能有几个选择

  • 我还有其他需要做的事情request.env['omniauth.auth']才能出现吗?我是否在其他地方向Google发送了回调以取回request.env['omniauth.auth']数据?
  • 实现omniauthable用户和admin_users都可以使用模型

有人看到我在做什么错吗,或者可以添加些什么?我要从google oauth中获得的只是该auth哈希,应该存在于中request.env['omniauth.auth']

谢谢您的帮助

詹姆斯·N

这是一个非常专业和孤立的问题,可能对运行此问题的其他人没有太大帮助。我在拥有各种Google密钥的组织内部制作了此应用。因此,对我来说,解决此问题的方法是使用正确的密钥。我之前使用的密钥不符合组织的设置,因此无法正常工作。一旦为该组织使用了正确的密钥,就可以正常工作。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Rails - Google oauth2 request.env['omniauth.auth'] is nil using omniauth with multiple models

request.env ['omniauth.auth']在Ruby on Rails中为零

在 rails API 中使用 omniauth-google-oauth2

OAuth2和Omniauth之间的区别

Rails,用于松弛的Omniauth:如何访问Auth Hash信息

Wordpress是否有可用的Devise / Omniauth / Oauth2接口?

redirect_uri_mismatch与Heroku上的OmniAuth Google oauth2

Rails设计专家omniauth

带有devise和omniauth的Google oauth2被视为失败

Rails 4 OmniAuth Facebook NoAuthorizationErrorCode

Rails中的Omniauth / MYSQL尴尬

Rails 4.1.5 omniauth强参数

Rails API Omniauth(Facebook)登录

Rails omniauth不刷新数据

Instagram在Rails上的Omniauth invalid_credentials

Google omniauth + devise + @域访问+ Rails

使用omniauth从Rails应用发布到Twitter

如何在Rails 4中保存Facebook omniauth的request.referrer

在Rails 3.2中使用OmniAuth-Twitter需要OAuth :: Unauthorized 401授权

如何正确注册和访问OAuth2的Office 365 Graph API(使用来自Ruby的omniauth)?

Omniauth_google_oauth2错误:redirect_uri_mismatch

获取头像 omniauth discord Ruby on Rails

Rails omniauth facebook SSL握手失败

Omniauth:“缺少模板错误”-Ruby on Rails

Rails 4,Devise,Omniauth,Cancan,Twitter API

Salesforce Sandbox Omniauth for Rails应用程序

Rails 4 Devise Omniauth,其中多个提供程序绑定到与用户关联的模型

在Rails中设置omniauth后访问Google Calendar API

Omniauth没有捕获初始的“ / auth /:provider”请求