diff --git a/src/lib.rs b/src/lib.rs index 4c4f66c..44412d1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,6 +12,7 @@ mod peertube; use peertube::{get_latest_video, get_max_resolution_dl}; mod youtube; +pub use youtube::register; use youtube::{create_resumable_upload, now_kiss}; async fn get_dl_video_stream( diff --git a/src/main.rs b/src/main.rs index 3f3b5cb..1efeb9a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,8 +17,30 @@ fn main() { .default_value(DEFAULT_CONFIG_PATH) .display_order(1), ) + .subcommand( + Command::new("register") + .version(env!("CARGO_PKG_VERSION")) + .about("Command to register to YouTube OAuth2.0") + .arg( + Arg::new("config") + .short('c') + .long("config") + .value_name("CONFIG_FILE") + .help("TOML config file for tootube") + .num_args(1) + .default_value(DEFAULT_CONFIG_PATH) + .display_order(1), + ), + ) .get_matches(); + if let Some(("register", sub_m)) = matches.subcommand() { + let config = parse_toml(sub_m.get_one::("config").unwrap()); + register(&config.youtube) + .unwrap_or_else(|e| panic!("Cannot register to YouTube API: {}", e)); + return; + } + let config = parse_toml(matches.get_one::("config").unwrap()); env_logger::init(); diff --git a/src/youtube.rs b/src/youtube.rs index b5bb729..9d971df 100644 --- a/src/youtube.rs +++ b/src/youtube.rs @@ -1,9 +1,9 @@ use crate::{config::YoutubeConfig, error::TootubeError, peertube::PeerTubeVideo}; use bytes::Bytes; use futures_core::stream::Stream; -use reqwest::{Body, Client}; +use reqwest::{multipart::Form, Body, Client}; use serde::{Deserialize, Serialize}; -use std::error::Error; +use std::{error::Error, io::stdin}; use tokio::sync::OnceCell; static ACCESS_TOKEN: OnceCell = OnceCell::const_new(); @@ -34,6 +34,11 @@ struct AccessTokenResponse { access_token: String, } +#[derive(Deserialize, Debug)] +struct RegistrationAccessTokenResponse { + refresh_token: String, +} + #[derive(Serialize, Debug)] struct YoutubeUploadParams { snippet: YoutubeUploadParamsSnippet, @@ -82,6 +87,41 @@ impl Default for YoutubeUploadParamsStatus { } } +/// This function makes the registration process a little bit easier +#[tokio::main] +pub async fn register(config: &YoutubeConfig) -> Result<(), Box> { + println!("Click on the link below to authorize {} to upload to YouTube and deal with your playlists:", env!("CARGO_PKG_NAME")); + println!("https://accounts.google.com/o/oauth2/v2/auth?client_id={}&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=https://www.googleapis.com/auth/youtube%20https://www.googleapis.com/auth/youtube.upload&response_type=code", config.client_id); + println!("Paste the returned authorization code:"); + + let mut input = String::new(); + stdin() + .read_line(&mut input) + .expect("Unable to read back the authorization code!"); + + let form = Form::new() + .text("code", input) + .text("client_id", config.client_id.clone()) + .text("client_secret", config.client_secret.clone()) + .text("redirect_uri", "urn:ietf:wg:oauth:2.0:oob") + .text("grant_type", "authorization_code"); + + let client = Client::new(); + let res = client + .post("https://accounts.google.com/o/oauth2/token") + .multipart(form) + .send() + .await?; + + let refresh_token: RegistrationAccessTokenResponse = res.json().await?; + + println!("You can now paste the following line inside the `youtube` section of your tootube.toml file:"); + + println!("refresh_token=\"{}\"", refresh_token.refresh_token); + + Ok(()) +} + /// Ensures that Token has been refreshed and that it is unique async fn refresh_token(config: &YoutubeConfig) -> Result { ACCESS_TOKEN