pub struct GoogleOAuth2Client {
pub client: Client<BasicErrorResponse, BasicTokenResponse, BasicTokenIntrospectionResponse, StandardRevocableToken, BasicRevocationErrorResponse, EndpointSet, EndpointNotSet, EndpointNotSet, EndpointNotSet, EndpointSet>,
}
Expand description
A thin wrapper around oauth2
for Google OAuth2.
ⓘ
use tlns_google_oauth2::GoogleOAuth2Client;
#[pollster::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = GoogleOAuth2Client::new(
"client_id",
"client_secret",
"redirect_uri",
)?;
let auth_url = client.build_authorize_url(
None,
&[&tlns_google_oauth2::grouped_scopes::GoogleOAuth2APIv2::AuthUserinfoProfile, &tlns_google_oauth2::scopes::Scopes::AuthUserinfoEmail],
);
println!("Redirect to {}", auth_url.redirect_url);
let token = client.get_token("code", None).await?;
println!("Token: {:?}", token);
Ok(())
}
Fields§
§client: Client<BasicErrorResponse, BasicTokenResponse, BasicTokenIntrospectionResponse, StandardRevocableToken, BasicRevocationErrorResponse, EndpointSet, EndpointNotSet, EndpointNotSet, EndpointNotSet, EndpointSet>
OAuth2 client
Implementations§
Source§impl GoogleOAuth2Client
impl GoogleOAuth2Client
Sourcepub fn new(
client_id: impl Into<String>,
client_secret: impl Into<String>,
redirect_uri: impl Into<String>,
) -> Result<Self, ParseError>
pub fn new( client_id: impl Into<String>, client_secret: impl Into<String>, redirect_uri: impl Into<String>, ) -> Result<Self, ParseError>
Create new crate::GoogleOAuth2Client
instance
Make a authorization URL for user to authenticate
csrf_token
will be default oauth2::CsrfToken::new_random
crate::grouped_scopes
example will be
&[&tlns_google_oauth2::grouped_scopes::GoogleOAuth2APIv2::AuthUserinfoProfile];
Or like this!
use std::str::FromStr;
use tlns_google_oauth2::FromGoogleScope;
&[&tlns_google_oauth2::scopes::Scopes::from_google_scope("https://www.googleapis.com/auth/userinfo.profile").unwrap()];
// or
&[&tlns_google_oauth2::scopes::Scopes::from_str("https://www.googleapis.com/auth/userinfo.profile").unwrap()];
Or if you are using crate::scopes
&[&tlns_google_oauth2::scopes::Scopes::AuthUserinfoProfile];
Sourcepub async fn get_token(
&self,
auth_code: impl Into<String>,
http_client: Option<&Client>,
) -> Result<StandardTokenResponse<EmptyExtraTokenFields, BasicTokenType>, RequestTokenError<HttpClientError<Error>, StandardErrorResponse<BasicErrorResponseType>>>
pub async fn get_token( &self, auth_code: impl Into<String>, http_client: Option<&Client>, ) -> Result<StandardTokenResponse<EmptyExtraTokenFields, BasicTokenType>, RequestTokenError<HttpClientError<Error>, StandardErrorResponse<BasicErrorResponseType>>>
Get authentication tokens from provider with authenticated code from Google Typically you will get it from the query string of the redirected URL
Sourcepub async fn refresh_token(
&self,
refresh_token: impl Into<String>,
http_client: Option<&Client>,
) -> Result<StandardTokenResponse<EmptyExtraTokenFields, BasicTokenType>, RequestTokenError<HttpClientError<Error>, StandardErrorResponse<BasicErrorResponseType>>>
pub async fn refresh_token( &self, refresh_token: impl Into<String>, http_client: Option<&Client>, ) -> Result<StandardTokenResponse<EmptyExtraTokenFields, BasicTokenType>, RequestTokenError<HttpClientError<Error>, StandardErrorResponse<BasicErrorResponseType>>>
Refresh your token in case it has expired
You can retrieve it from oauth2::StandardTokenResponse::refresh_token
Trait Implementations§
Source§impl Clone for GoogleOAuth2Client
impl Clone for GoogleOAuth2Client
Source§fn clone(&self) -> GoogleOAuth2Client
fn clone(&self) -> GoogleOAuth2Client
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreAuto Trait Implementations§
impl Freeze for GoogleOAuth2Client
impl RefUnwindSafe for GoogleOAuth2Client
impl Send for GoogleOAuth2Client
impl Sync for GoogleOAuth2Client
impl Unpin for GoogleOAuth2Client
impl UnwindSafe for GoogleOAuth2Client
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more