use chrono::{DateTime, Utc};
use ghtkn::browser::BrowserError;
use ghtkn::deviceflow::DeviceCodeResponse;
use ghtkn::{Browser, Client, DeviceCodeUI, InputGet};
struct JsonDeviceCodeUI;
impl DeviceCodeUI for JsonDeviceCodeUI {
fn show(
&self,
device_code: &DeviceCodeResponse,
expiration_date: DateTime<Utc>,
) -> ghtkn::Result<()> {
let json = serde_json::json!({
"user_code": device_code.user_code,
"verification_uri": device_code.verification_uri,
"expires_at": expiration_date.to_rfc3339(),
});
eprintln!("{}", serde_json::to_string_pretty(&json).unwrap());
Ok(())
}
}
struct PrintOnlyBrowser;
impl Browser for PrintOnlyBrowser {
fn open(&self, url: &str) -> Result<(), BrowserError> {
eprintln!("Please open this URL in your browser: {url}");
Ok(())
}
}
#[tokio::main]
async fn main() {
let mut client = Client::new();
client.set_device_code_ui(Box::new(JsonDeviceCodeUI));
client.set_browser(Box::new(PrintOnlyBrowser));
let input = InputGet::default();
match client.get(&input).await {
Ok((token, app)) => {
println!("App: {}", app.name);
println!("User: {}", token.login);
println!(
"Token: {}...",
&token.access_token[..token.access_token.len().min(8)]
);
}
Err(e) => eprintln!("Error: {e}"),
}
}