Skip to content

Commit

Permalink
Update examples and add cookie examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jellejurre committed Nov 25, 2024
1 parent 59c59c7 commit c2d3c81
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 3 deletions.
31 changes: 31 additions & 0 deletions examples/cookies_load.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use std::io::{self, Write};
use reqwest::header::{HeaderMap, HeaderValue};
pub use vrchatapi::apis;
use vrchatapi::models::{EitherUserOrTwoFactor, TwoFactorAuthCode, TwoFactorEmailCode};

#[tokio::main]
async fn main() {
let mut config = apis::configuration::Configuration::default();
config.basic_auth = Some((String::from("username"), Some(String::from("password"))));
config.user_agent = Some(String::from("ProjectName/0.0.1 [email protected]"));

let mut header_map = HeaderMap::new();
header_map.append("Cookie", HeaderValue::from_str(
&"auth=[AUTH_COOKIE_HERE]; twoFactorAuth=[TWO_FACTOR_AUTH_COOKIE_HERE]"
).unwrap());

config.client = reqwest::Client::builder()
.cookie_store(true)
.default_headers(header_map)
.build()
.unwrap();

let user = apis::authentication_api::get_current_user(&config)
.await
.unwrap();

match user {
EitherUserOrTwoFactor::CurrentUser(user) => println!("Current user: {}", user.display_name),
EitherUserOrTwoFactor::RequiresTwoFactorAuth(_) => println!("cookie invalid")
}
}
76 changes: 76 additions & 0 deletions examples/cookies_store.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use std::io::{self, Write};
use std::str::FromStr;
use reqwest::cookie::CookieStore;
use url::Url;
pub use vrchatapi::apis;
use vrchatapi::models::{EitherUserOrTwoFactor, TwoFactorAuthCode, TwoFactorEmailCode};

#[tokio::main]
async fn main() {
let mut config = apis::configuration::Configuration::default();
config.basic_auth = Some((String::from("username"), Some(String::from("password"))));
config.user_agent = Some(String::from("ProjectName/0.0.1 [email protected]"));

let cookie_store = std::sync::Arc::new(reqwest::cookie::Jar::default());
config.client = reqwest::Client::builder()
.cookie_store(true)
.cookie_provider(cookie_store.clone())
.build()
.unwrap();

match apis::authentication_api::get_current_user(&config)
.await
.unwrap()
{
vrchatapi::models::EitherUserOrTwoFactor::CurrentUser(me) => {
println!("Username: {}", me.username.unwrap())
}
vrchatapi::models::EitherUserOrTwoFactor::RequiresTwoFactorAuth(requires_auth) => {
if requires_auth.requires_two_factor_auth.contains(&String::from("emailOtp")){
let code = read_user_input("Please enter your Email 2fa code: ");
if let Err(err) = apis::authentication_api::verify2_fa_email_code(
&config,
TwoFactorEmailCode::new(code),
)
.await
{
eprintln!("Error verifying 2FA email code: {}", err);
}
} else {
let code = read_user_input("Please enter your Authenticator 2fa code: ");
if let Err(err) = apis::authentication_api::verify2_fa(
&config,
TwoFactorAuthCode::new(code),
)
.await
{
eprintln!("Error verifying 2FA auth code: {}", err);
}
}

}
}

let user = apis::authentication_api::get_current_user(&config)
.await
.unwrap();

match user {
EitherUserOrTwoFactor::CurrentUser(user) => println!("Current user: {}", user.display_name),
EitherUserOrTwoFactor::RequiresTwoFactorAuth(_) => println!("cookie invalid")
}

println!("Cookie:{}", cookie_store.cookies(&Url::from_str("https://api.vrchat.cloud").expect("Url not okay")).expect("Cookies not found").to_str().expect("Cookies not valid string"));
}

fn read_user_input(prompt: &str) -> String {
print!("{}", prompt);
io::stdout().flush().expect("Failed to flush stdout");

let mut input = String::new();
io::stdin()
.read_line(&mut input)
.expect("Failed to read line");

input.trim().to_string()
}
10 changes: 7 additions & 3 deletions examples/online.rs → examples/example.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::io::{self, Write};
pub use vrchatapi::apis;
use vrchatapi::models::{TwoFactorAuthCode, TwoFactorEmailCode};
use vrchatapi::models::{EitherUserOrTwoFactor, TwoFactorAuthCode, TwoFactorEmailCode};

#[tokio::main]
async fn main() {
Expand Down Expand Up @@ -40,10 +40,14 @@ async fn main() {
}
}

let online = apis::system_api::get_current_online_users(&config)
let user = apis::authentication_api::get_current_user(&config)
.await
.unwrap();
println!("Current Online Users: {}", online);

match user {
EitherUserOrTwoFactor::CurrentUser(user) => println!("Current user: {}", user.display_name),
EitherUserOrTwoFactor::RequiresTwoFactorAuth(_) => println!("cookie invalid")
}
}

fn read_user_input(prompt: &str) -> String {
Expand Down

0 comments on commit c2d3c81

Please sign in to comment.