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

Automatic token refreshing #261

Open
vvitto opened this issue Oct 19, 2023 · 2 comments
Open

Automatic token refreshing #261

vvitto opened this issue Oct 19, 2023 · 2 comments
Assignees

Comments

@vvitto
Copy link

vvitto commented Oct 19, 2023

Hi. I've investigated codebase of the gem and seems there is not mechanism for auto refreshing access token when it gets expired. I use the hubspot client in many services and it becomes a little annoying to copy paste code with handing exceptions. Here is a basic example of how it works:

def call
    client = ::Hubspot::Client.new(access_token: integration.access_token)
    client.crm.contacts.basic_api.get_by_id(contact_id:)
   ...
rescue Hubspot::Crm::Contacts::ApiError => e
    if expired?(e)
      Integration::HubspotIntegration::RefreshAccessToken.call(integration:)
      retry
    end
end

def expired?(error)
    JSON.parse(error.response_body)['category'] == 'EXPIRED_AUTHENTICATION'
end

So what would be nice to have:

  • Automatically refresh access token once it gets expired.
  • Retry request one more time with fresh access token.
  • Be able to pass lambda or proc into constructor that will be called with refreshed token.

Example:

client = ::Hubspot::Client.new(
   access_token: integration.access_token,
   refresh_token: integration.refresh_token,
   client_id: integration.client_id,
   client_secret: integration.client_secret,
   redirect_uri: integration.redirect.uri,
   authentication_callback: ->(response) do
        integration.update!(
          access_token: response.access_token,
          refresh_token: response.refresh_token
        )
)

I can add this feature by myself if you don't mind and can guide me a little bit, or maybe you know some other ways how to solve my problem?

@alzheltkovskiy-hubspot
Copy link
Collaborator

Hi @vvitto. Thank you for the interesting proposal, we will discuss this with the team and give you an answer.

@alzheltkovskiy-hubspot
Copy link
Collaborator

Hi @vvitto. We discussed this with the team and came to the conclusion that this is not a bad idea. We will try to implement it after we resolve our current issues.

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