我试图获得离线访问令牌(refresh_token)与全auth google-oauth2策略。
这是我的全权限初始化代码:
Rails.application.config.middleware.use OmniAuth::Builder do
provider :google_oauth2, KEY, SECRET,
:access_type => 'offline',
:scope => 'https://www.googleapis.com/auth/userinfo.profile'
end
当重定向到google进行oauth2身份验证时,它应该添加一个额外的URL参数,如&access_type=offline,但它没有这样做(如果我手动添加参数,它可以正常工作)。
我错过了什么吗?
如果您正在使用Omniauth与zquestz的google_oauth2策略,那么access_type的默认值在未指定的情况下为offline。
From his github in *omniauth/strategies/oauth2/google_oauth2*:
def authorize_params
base_scope_url = "https://www.googleapis.com/auth/"
super.tap do |params|
scopes = (params[:scope] || DEFAULT_SCOPE).split(",")
scopes.map! { |s| s =~ /^https?:/// ? s : "#{base_scope_url}#{s}" }
params[:scope] = scopes.join(' ')
# This makes sure we get a refresh_token.
# http://googlecode.blogspot.com/2011/10/upcoming-changes-to-oauth-20-endpoint.html
**params[:access_type] = 'offline' if params[:access_type].nil?
params[:approval_prompt] = 'force' if params[:approval_prompt].nil?**
end
end
作为旁注,我认为scope字段应该是散列的:{:scope => userinfo。概要}。
通过将zquestz/omniauth-google-oauth2升级到0.1.8版本修复了此问题。