我正在使用Rails应用程序,并使用omniauth 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
有对色器件/ omniauth各种途径
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']
。
谢谢您的帮助
这是一个非常专业和孤立的问题,可能对运行此问题的其他人没有太大帮助。我在拥有各种Google密钥的组织内部制作了此应用。因此,对我来说,解决此问题的方法是使用正确的密钥。我之前使用的密钥不符合组织的设置,因此无法正常工作。一旦为该组织使用了正确的密钥,就可以正常工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句