Skip to content

Access token doesn't refresh. #400

@thomasdziedzic

Description

@thomasdziedzic

Environment details

  • OS: ubuntu 22.04 (on wsl)
  • Ruby version: 3.1.2
  • Gem name and version: googleauth 1.3.0

Steps to reproduce

  1. Get credentials using the library.
  2. Let credentials expire.
  3. When making a request using the library with the expired credentials, it doesn't refresh the access token, causing invalid token response.

Code example

class GoogleCalendar
  def self.authorizer
    @@authorizer ||= begin
      client_id = Google::Auth::ClientId.new(Rails.application.credentials.google.client_id, Rails.application.credentials.google.client_secret)
      scope = ['https://www.googleapis.com/auth/calendar.events']
      token_store =  DatabaseTokenStore.new

      Google::Auth::WebUserAuthorizer.new(client_id, scope, token_store, '/oauth2/callback')
    end
  end
end
require 'googleauth/token_store'

class DatabaseTokenStore < Google::Auth::TokenStore
  def load(id)
    user = User.find(id)
    user.google_oauth2_tokens
  end

  def store(id, tokens)
    user = User.find(id)
    user.google_oauth2_tokens = tokens
    user.save!
  end

  def delete(id)
    user = User.find(id)
    user.google_oauth2_tokens = nil
    user.save!
  end
end
    credentials = GoogleCalendar.authorizer.get_credentials(user_id, request)

    if credentials.nil?
      render plain: 'Google is not connected.'
      return
    end

    service = Google::Apis::CalendarV3::CalendarService.new
    service.authorization = credentials

    response = service.list_events('an@example.email', time_min: Time.current.iso8601)

Making sure to follow these steps will guarantee the quickest resolution possible.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions