Skip to content
This repository was archived by the owner on Oct 8, 2025. It is now read-only.
This repository was archived by the owner on Oct 8, 2025. It is now read-only.

GoogleRefreshTokenProvider should refresh the token if it's expires #63

Description

@omerlh

Thanks for stopping by to let us know something could be better!

PLEASE READ: If you have a support contract with Google, please create an issue in the support console instead of filing on GitHub. This will ensure a timely response.

Is your feature request related to a problem? Please describe.
When having a long running code, the access token will expire and will not be automatically refreshed.

Describe the solution you'd like

withToken method should check if the token expire, and if so, try to refresh it using exchange instead of always returning the token.

Describe alternatives you've considered

I have a working solution that looks like that:

private var compute: Compute {
        get async throws {
            if let tokenProvider = tokenProvider {
                
                let token: Token = try await withCheckedThrowingContinuation{ continuation in
                    do {
                        try tokenProvider.withToken{ token, error in
                            if let token = token {
                                continuation.resume(returning: token)
                            } else if let error = error {
                                continuation.resume(throwing: error)
                            }
                        }
                    } catch{
                        continuation.resume(throwing: error)
                    }
                }
                
                if let creationTime = self.tokenCreationTime, let expiresIn = token.ExpiresIn {
                    let expirationDate = creationTime.advanced(by: Double(expiresIn * 60))
                    
                    print(expirationDate)
                    print(Date().advanced(by: -(5 * 60)))
                    
                    if (expirationDate > Date().advanced(by: -(5 * 60))) {
                        self.tokenProvider = try self.tokenProviderFactory()
                        self.tokenCreationTime = Date()
                    }
                }
                
                return Compute(tokenProvider: tokenProvider)
            } else {
                let tokenProvider = try self.tokenProviderFactory()
                self.tokenProvider = tokenProvider
                self.tokenCreationTime = Date()
                return Compute(tokenProvider: tokenProvider)
            }
        }
    }

But I think this is better suited in the library itself as this is a common problem...

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority: p3Desirable enhancement or fix. May not be included in next release.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions