Oauth2 google-api-ruby-client: How to set Approval Prompt to Auto?

社会主义新天地 提交于 2019-12-11 03:55:07

问题


Question: How to set the Approval Prompt to Auto? It defaults to 'approval_prompt=force'

Code: I am setting up the client like this.

   @client = Google::APIClient.new(
     :authorization => :oauth_2,
     :host => 'www.googleapis.com',
     :http_adapter => HTTPAdapter::NetHTTPAdapter.new
   )   
   @client.authorization.client_id = 'xxxx.apps.googleusercontent.com'
   @client.authorization.client_secret = 'xxxx'

Context: Google OAuth2

Client Library: google-api-ruby-client

Reference: Same question for the php client :
Google+ OAuth API store and retrieve tokens after first login and authorization

Signet Documentation. I can't find the approval_prompt setter http://signet.rubyforge.org/api/Signet/OAuth2/Client.html


回答1:


This is how I solved the problem.

I wrote a separate helper method that will generate the Google OAuth URI

def build_auth_uri
return @client.authorization.authorization_uri(
 :approval_prompt => :auto
).to_s 

end

Next, instead of referring to Google OAuth URI directly in my view, I called the helper.

That did the trick.




回答2:


This is how I solved the problem:

In /app/views/devise/shared/_links.haml (it's similar for _links.erb):

- if devise_mapping.omniauthable?
  - resource_class.omniauth_providers.each do |provider|
    - if provider == :google_oauth2
      = link_to "Sign in with Google", omniauth_authorize_path(resource_name, provider, approval_prompt: :auto)
    - else
      = link_to "Sign in with #{provider.to_s.titleize}", omniauth_authorize_path(resource_name, provider)
    %br/

EDIT: Even easier: Add this to your devise.rb or omniauth.rb initializer (in /config/initializers):

provider :google_oauth2, ENV["GOOGLE_KEY"], ENV["GOOGLE_SECRET"], {
  approval_prompt: "auto"
}

Check the documentation here for more info.



来源:https://stackoverflow.com/questions/11389382/oauth2-google-api-ruby-client-how-to-set-approval-prompt-to-auto

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!