簡體   English   中英

Omniauth谷歌oauth2戰略與離線訪問

[英]Omniauth google oauth2 strategy with offline access

我正在嘗試使用omniauth google-oauth2策略獲取離線訪問令牌(refresh_token)。

這是我的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

當重定向到谷歌進行oauth2身份驗證時,它應該添加一個額外的URL參數,如&access_type = offline,但它無法這樣做(如果我手動添加參數,它可以正常工作)。

我錯過了什么嗎?

如果您將Omniauth與zquestz的google_oauth2策略一起使用,則access_type的默認值如果未指定則處於脫機狀態。

從他在* 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

作為旁注,我認為范圍字段應該是哈希:{:scope => userinfo.profile}。

通過將zquestz / omniauth-google-oauth2升級到版本0.1.8來解決此問題。 顯然,此問題僅出現在0.1.7中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM