Merge pull request #19 from simplymeasured/feature/make-autorefresh-of-token-optional

Make the auto-refresh of tokens configurable and optional
This commit is contained in:
Steve Bazyl 2012-12-30 11:21:56 -08:00
commit 6b9d4bc879
1 changed files with 13 additions and 1 deletions

View File

@ -47,6 +47,10 @@ module Google
# <li><code>:oauth_1</code></li> # <li><code>:oauth_1</code></li>
# <li><code>:oauth_2</code></li> # <li><code>:oauth_2</code></li>
# </ul> # </ul>
# @option options [Boolean] :auto_refresh_token (true)
# The setting that controls whether or not the api client attempts to
# refresh authorization when a 401 is hit in #execute. If the token does
# not support it, this option is ignored.
# @option options [String] :application_name # @option options [String] :application_name
# The name of the application using the client. # The name of the application using the client.
# @option options [String] :application_version # @option options [String] :application_version
@ -89,6 +93,7 @@ module Google
# default authentication mechanisms. # default authentication mechanisms.
self.authorization = self.authorization =
options.key?(:authorization) ? options[:authorization] : :oauth_2 options.key?(:authorization) ? options[:authorization] : :oauth_2
self.auto_refresh_token = options.fetch(:auto_refresh_token){ true }
self.key = options[:key] self.key = options[:key]
self.user_ip = options[:user_ip] self.user_ip = options[:user_ip]
@discovery_uris = {} @discovery_uris = {}
@ -159,6 +164,13 @@ module Google
# @return [String] The API key. # @return [String] The API key.
attr_accessor :key attr_accessor :key
##
# The setting that controls whether or not the api client attempts to
# refresh authorization when a 401 is hit in #execute.
#
# @return [Boolean]
attr_accessor :auto_refresh_token
## ##
# The IP address of the user this request is being performed on behalf of. # The IP address of the user this request is being performed on behalf of.
# #
@ -540,7 +552,7 @@ module Google
request.authorization = options[:authorization] || self.authorization unless options[:authenticated] == false request.authorization = options[:authorization] || self.authorization unless options[:authenticated] == false
result = request.send(connection) result = request.send(connection)
if result.status == 401 && authorization.respond_to?(:refresh_token) if result.status == 401 && authorization.respond_to?(:refresh_token) && auto_refresh_token
begin begin
authorization.fetch_access_token! authorization.fetch_access_token!
result = request.send(connection) result = request.send(connection)