Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access token doesn't refresh. #400

Open
thomasdziedzic opened this issue Nov 21, 2022 · 1 comment
Open

Access token doesn't refresh. #400

thomasdziedzic opened this issue Nov 21, 2022 · 1 comment

Comments

@thomasdziedzic
Copy link

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('[email protected]', time_min: Time.current.iso8601)

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

Thanks!

@jjatinggoyal
Copy link

@thomasdziedzic how did you resolve this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants