pub struct DeviceUserTokenBuilder { /* private fields */ }
Expand description
Builder for OAuth device code flow
§Examples
let client = reqwest::Client::builder()
.redirect(reqwest::redirect::Policy::none())
.build()?;
let mut builder = DeviceUserTokenBuilder::new("myclientid", vec![Scope::ChatRead, Scope::ChatEdit]);
let code = builder.start(&client).await?;
println!("Please go to {}", code.verification_uri);
let token = builder.wait_for_code(&client, tokio::time::sleep).await?;
println!("Token: {:?}", token);
Implementations§
Source§impl DeviceUserTokenBuilder
impl DeviceUserTokenBuilder
Sourcepub fn new(client_id: impl Into<ClientId>, scopes: Vec<Scope>) -> Self
pub fn new(client_id: impl Into<ClientId>, scopes: Vec<Scope>) -> Self
Create a DeviceUserTokenBuilder
Sourcepub fn set_secret(&mut self, secret: Option<ClientSecret>)
pub fn set_secret(&mut self, secret: Option<ClientSecret>)
Set the client secret, only necessary if you have one
Sourcepub fn get_exchange_device_code_request(&self) -> Request<Vec<u8>>
pub fn get_exchange_device_code_request(&self) -> Request<Vec<u8>>
Get the request for getting a DeviceCodeResponse
Sourcepub fn parse_exchange_device_code_response(
&mut self,
response: Response<Vec<u8>>,
) -> Result<&DeviceCodeResponse, RequestParseError>
pub fn parse_exchange_device_code_response( &mut self, response: Response<Vec<u8>>, ) -> Result<&DeviceCodeResponse, RequestParseError>
Parse the response from the device code request
Sourcepub async fn start<'a, 's, C>(
&'s mut self,
http_client: &'a C,
) -> Result<&'s DeviceCodeResponse, DeviceUserTokenExchangeError<C::Error>>where
C: Client,
Available on crate feature client
only.
pub async fn start<'a, 's, C>(
&'s mut self,
http_client: &'a C,
) -> Result<&'s DeviceCodeResponse, DeviceUserTokenExchangeError<C::Error>>where
C: Client,
client
only.Start the device code flow
§Notes
Use DeviceCodeResponse::verification_uri
to get the URL the user needs to visit.
Sourcepub fn get_user_token_request(&self) -> Option<Request<Vec<u8>>>
pub fn get_user_token_request(&self) -> Option<Request<Vec<u8>>>
Get the request for getting a TwitchTokenResponse
, to be used in UserToken::from_response.
Returns None if there is no device_code
Sourcepub async fn wait_for_code<'a, C, Fut>(
&mut self,
client: &'a C,
wait_fn: impl Fn(Duration) -> Fut,
) -> Result<UserToken, DeviceUserTokenExchangeError<C::Error>>
Available on crate feature client
only.
pub async fn wait_for_code<'a, C, Fut>( &mut self, client: &'a C, wait_fn: impl Fn(Duration) -> Fut, ) -> Result<UserToken, DeviceUserTokenExchangeError<C::Error>>
client
only.Finish the device code flow by waiting for the user to authorize, granting you a token if the user has authorized the app.
Will return DeviceUserTokenExchangeError::Expired
if the user has not authorized the app within the expires_in
time.
§Examples
let client = reqwest::Client::builder()
.redirect(reqwest::redirect::Policy::none())
.build()?;
let mut builder = DeviceUserTokenBuilder::new("myclientid", vec![Scope::ChatRead, Scope::ChatEdit]);
let code = builder.start(&client).await?;
println!("Please go to {}", code.verification_uri);
let token = builder.wait_for_code(&client, tokio::time::sleep).await?;
println!("Token: {:?}", token);
Sourcepub async fn try_finish<'a, C>(
&self,
http_client: &'a C,
) -> Result<UserToken, DeviceUserTokenExchangeError<C::Error>>where
C: Client,
Available on crate feature client
only.
pub async fn try_finish<'a, C>(
&self,
http_client: &'a C,
) -> Result<UserToken, DeviceUserTokenExchangeError<C::Error>>where
C: Client,
client
only.Finish the device code flow, granting you a token if the user has authorized the app.
Consider using the wait_for_code
method instead.
§Notes
Must be called after start
and will return an error if not.
The error could be that the user has not authorized the app yet, in which case you should wait for interval
seconds and try again.
To check for this condition, use the is_pending
method.
§Examples
let client = reqwest::Client::builder()
.redirect(reqwest::redirect::Policy::none())
.build()?;
let code = builder.start(&client).await?;
println!("Please go to {}", code.verification_uri);
let mut interval = tokio::time::interval(std::time::Duration::from_secs(code.interval));
let mut finish = builder.try_finish(&client).await;
while finish.as_ref().is_err_and(|e| e.is_pending()) {
// wait a bit
interval.tick().await;
finish = builder.try_finish(&client).await;
}
let token: UserToken = finish?;