Expand description
Authentication for Google API endpoints
Allows users to provide custom authentication implementations to suite their needs.
By default, GetToken
is implemented for:
Authenticator
: An authenticator which supports a variety of authentication methodsString
: Plain OAuth2 token in String formatNoToken
: No token, used for APIs which do not require a token
§Usage
GetToken
instances are designed to be used with the Hub constructor provided by the
generated APIs.
If you intend to use the API libraries on client devices,
Authenticator
supports a variety of client-side authentication methods,
and should be used to provide authentication.
If you intend to use the API libraries server-side, with server-side client authentication,
use the oauth2
crate and convert the resulting AccessToken
to String
.
If you intend to use APIs which do not require authentication, use NoToken
.
If you have custom authentication requirements, you can implement GetToken
manually.
§Example
use std::future::Future;
use std::pin::Pin;
use google_apis_common::GetToken;
use yup_oauth2::authenticator::Authenticator;
#[derive(Clone)]
struct AuthenticatorWithRetry<S> {
auth: Authenticator<S>,
retries: usize,
}
impl<C> GetToken for AuthenticatorWithRetry<C>
where
C: hyper_util::client::legacy::connect::Connect + Clone + Send + Sync + 'static,
{
fn get_token<'a>(
&'a self,
scopes: &'a [&str],
) -> Pin<Box<dyn Future<Output = Result<Option<String>, Box<dyn std::error::Error + Send + Sync>>> + Send + 'a>> {
Box::pin(async move {
let mut auth_token = Ok(None);
for _ in 0..=self.retries {
match self.auth.token(scopes).await {
Ok(token) => {
auth_token = Ok(token.token().map(|t| t.to_owned()));
break;
},
Err(e) => auth_token = Err(e.into()),
}
}
auth_token
})
}
}
Structs§
- In the event that the API endpoint does not require an OAuth2 token,
NoToken
should be provided to the hub to avoid specifying an authenticator.