How to automatically refresh token when access token expires (receiving 401) ?

127 views
Skip to first unread message

Tao Zhang

unread,
Sep 1, 2015, 4:00:02 AM9/1/15
to Google API Client for Ruby
Hi!
I'm using the google-api-ruby-client gem, doing the basic calling such as:

      @client = Google::Apis::DriveV2::DriveService.new
      @client.authorization = @authorization
      @client.list_files()

At first I thought that when the access token expires, it will automatically refresh the it using the refresh_token
(and I store the refresh token in database for each user),
as I see in google-api-client-0.9.pre3/lib/google/apis/core/http_command.rb:
        def execute(client)
          prepare!
          proc = block_given? ? Proc.new : nil
          begin
            Retriable.retriable tries: options.retries + 1,
                                base_interval: 1,
                                multiplier: 2,
                                on: RETRIABLE_ERRORS do |try|
              # This 2nd level retriable only catches auth errors, and supports 1 retry, which allows
              # auth to be re-attempted without having to retry all sorts of other failures like
              # NotFound, etc
              auth_tries = (try == 1 && authorization_refreshable? ? 2 : 1)
              Retriable.retriable tries: auth_tries,
                                  on: [Google::Apis::AuthorizationError],
                                  on_retry: proc { |*| refresh_authorization } do
                return execute_once(client, &proc)
              end
            end
         .....

But this refresh_authorization method is defined as follows:

# Refresh the authorization authorization after a 401 error
#
# @private
# @return [void]
def refresh_authorization
# Handled implicitly by auth lib, here in case need to override
logger.debug('Retrying after authentication failure')
end

Every time only this line is executed, printing this line in the log, but nothing else is done, 
and I don't see how that is "handled implicitly by auth lib".

Am I missing some configurations?
Or is there anything else I need to implement myself in order for the automatic refreshing to work?
Thanks!

Message has been deleted

Tao Zhang

unread,
Sep 1, 2015, 4:31:04 AM9/1/15
to Google API Client for Ruby
I searched all the API documentations, but didn't find anything related to token refreshing
Message has been deleted

Tao Zhang

unread,
Sep 1, 2015, 5:50:08 AM9/1/15
to Google API Client for Ruby
I just changed the method to :
def refresh_authorization
options.authorization.refresh!
end

and the refreshing works!
Is there a dedicated place to override this method, instead of having to monkey-patch it?

Qasim Qureshi

unread,
Apr 23, 2017, 1:34:46 AM4/23/17
to Google API Client for Ruby
Google's APIs also have "offline access" which lets access tokens be refreshed without having the user log-in again and again. Not sure if that's compatible with the ruby implementation though (which is in alpha).
Reply all
Reply to author
Forward
0 new messages