我正在使用omniauth-google-oauth2 gem 来授权google 帐户,并获取授权码,但即使将gem 升级到最新版本后, request.env['omniauth.auth'] 仍为nil 。
ruby_version: 2.4.9
rails _version: 4.2.8
我的宝石文件
gem 'omniauth', '1.9.1'
gem 'omniauth-oauth2', '1.7.0'
gem 'omniauth-google-oauth2', '0.8.0'
全域认证.rb
Rails.application.config.middleware.use OmniAuth::Builder do
provider :google_oauth2, 'MY_GOOGLE_CLIENT_ID', 'MY_GOOGLE_SECRET', {
scope: 'email, profile',
access_type: "offline",
approval_prompt: "force",
prompt: 'select_account',
redirect_uri: 'http://localhost:3000/auth/google-oauth2/callback'
}
结尾
路线.rb
get 'auth/google_oauth2/callback', to: 'social_accounts#create_account'
socual_accounts_controller.rb
class Api::V1::SocialAccountsController < ApplicationController
def create_account
request_params = request.env["omniauth.auth"] # gives nil
end
end
是否可以从授权码中获取 access_token 和 refresh_token ?这也会有很大帮助
提前致谢