简体   繁体   English

Omniauth谷歌oauth2战略与离线访问

[英]Omniauth google oauth2 strategy with offline access

I'm trying to get offline access token (refresh_token) with omniauth google-oauth2 strategy. 我正在尝试使用omniauth google-oauth2策略获取离线访问令牌(refresh_token)。

This is my omniauth initializer code: 这是我的omniauth初始化代码:

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

When redirecting to google for the oauth2 authentication, it should add an extra URL parameter like &access_type=offline, but it fails to do so (it works fine if I add the parameter manually). 当重定向到谷歌进行oauth2身份验证时,它应该添加一个额外的URL参数,如&access_type = offline,但它无法这样做(如果我手动添加参数,它可以正常工作)。

Am I missing something? 我错过了什么吗?

If you are using Omniauth with zquestz's google_oauth2 strategy, then the default value for the access_type is offline if it is not specified. 如果您将Omniauth与zquestz的google_oauth2策略一起使用,则access_type的默认值如果未指定则处于脱机状态。

From his github in *omniauth/strategies/oauth2/google_oauth2*: 从他在* omniauth / strategies / oauth2 / google_oauth2 *中的github中:

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

As a side note I believe the scope field is supposed to be in a hash: { :scope => userinfo.profile }. 作为旁注,我认为范围字段应该是哈希:{:scope => userinfo.profile}。

Fixed this by upgrading zquestz/omniauth-google-oauth2 to version 0.1.8. 通过将zquestz / omniauth-google-oauth2升级到版本0.1.8来解决此问题。 Apparently this problem only occurs in 0.1.7. 显然,此问题仅出现在0.1.7中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM