dermesser / yup-oauth2

An oauth2 client implementation providing the Device, Installed, Service Account, and several more flows.
https://docs.rs/yup-oauth2/
Apache License 2.0
213 stars 114 forks source link

Providing a RefreshToken for the AccessTokenAuthenticator #203

Open JasterV opened 1 year ago

JasterV commented 1 year ago

Hi!

I have the following use case:

I generate manually both the token and access token to access the Gmail API. Therefore, I want to use the AccessTokenAuthenticator, but I see it only accepts an access_token string, then I understand that there is no internal logic that will refresh the token automatically once the token expires:(

Is there a way I can take advantage of the refresh token that I have so the token gets internally refreshed?

Here is my code

        let authenticator = oauth2::authenticator::AccessTokenAuthenticator::builder(
            // It would be nice to pass the refresh token in here
            self.access_token.token().to_owned(),
        )
        .build()
        .await
        .map_err(AuthError::AuthenticatorBuilderError)?;

        let mut hub = Gmail::new(
            hyper::Client::builder().build(
                hyper_rustls::HttpsConnectorBuilder::new()
                    .with_native_roots()
                    .https_or_http()
                    .enable_http1()
                    .enable_http2()
                    .build(),
            ),
            authenticator,
        );
JasterV commented 1 year ago

It would be also nice to be able to read the tokens directly from a file and do something like


// Get an ApplicationSecret instance by some means. It contains the `client_id` and 
// `client_secret`, among other things.
let token_info: TokenInfo = oauth2::read_token_from("/path/to/file");

  let authenticator = oauth2::authenticator::AccessTokenAuthenticator::builder(
            token_info
        )
        .persist_tokens_on_disk("path/to/file")
        .build()
        .await
        .map_err(AuthError::AuthenticatorBuilderError)?;