问题
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