为了验证用户,我在 rails 中使用 omniauth-google-oauth2 gem。
现在我想提供额外的 google api 功能,但我不想强制执行。但我想不出一种方法来使特定范围成为可选。
我尝试在 omniauth.rb 中添加另一个条目,但它似乎不允许我为同一个提供程序 (google_oauth2) 添加多个条目。
有没有办法在 rails 应用程序中添加任何可选范围?
最佳答案
您可以使用 name
选项。这应该适用于任何 OmniAuth 提供商:
Rails.application.config.middleware.use OmniAuth::Builder do
provider :google_oauth2, 'GOOGLE_CLIENT_ID', 'GOOGLE_CLIENT_SECRET', {
:name => 'google'
}
provider :google_oauth2, 'GOOGLE_CLIENT_ID', 'GOOGLE_CLIENT_SECRET', {
:name => 'google_full',
:scope => 'original_scope, extra_scope'
}
end
但是,使用 omniauth-google-oauth2,您可以采取另一种方法:
Rails.application.config.middleware.use OmniAuth::Builder do
provider :google_oauth2, 'GOOGLE_CLIENT_ID', 'GOOGLE_CLIENT_SECRET', {
:name => 'google'
}
provider :google_oauth2, 'GOOGLE_CLIENT_ID', 'GOOGLE_CLIENT_SECRET', {
:name => 'google_full',
:scope => 'extra_scope',
:prompt => 'consent',
:include_granted_scopes => 'true'
}
end
退房 Google's Incremental Authorization guide了解更多详情。
但请注意,通过更改
name
提供者的 OmniAuth URL 将更改为 /auth/new_name
和 request.env['omniauth.auth']['provider']
然后将返回 new_name
.
关于ruby-on-rails - oauth2 omniauth.rb 中的可选范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19350868/