From 52bbc25abc6ebd76f1318544accf6590aeb9bc9c Mon Sep 17 00:00:00 2001 From: Vsevolod Date: Wed, 4 Dec 2024 21:36:11 +0100 Subject: [PATCH 1/5] secret description fixed --- module/move/gspread/.key/readme.md | 44 --------------------------- module/move/gspread/.secret/readme.md | 39 ++++++++++++++++++++++++ module/move/gspread/src/bin/main.rs | 2 +- module/move/gspread/src/secret.rs | 14 ++++----- 4 files changed, 47 insertions(+), 52 deletions(-) delete mode 100644 module/move/gspread/.key/readme.md create mode 100644 module/move/gspread/.secret/readme.md diff --git a/module/move/gspread/.key/readme.md b/module/move/gspread/.key/readme.md deleted file mode 100644 index cfd1cc23d2..0000000000 --- a/module/move/gspread/.key/readme.md +++ /dev/null @@ -1,44 +0,0 @@ -# Getting API Keys for OAuth Authentication - -Follow these steps to create and configure your OAuth credentials for using Google APIs. - -## 1. Create API Credentials - -1. Go to the [Google API Console](https://console.developers.google.com/). -2. From the projects list, select an existing project or create a new one. -3. In the left side menu, select **APIs & Services**. -4. On the left menu, click **Credentials**. -5. Click **Create Credentials** and select **OAuth client ID**. -6. In the **Application type** section, select **Desktop app**. -7. Provide an appropriate name for your client ID (e.g., "MyApp OAuth Client"). -8. Click **Create**. - -Once the credential is created, you will receive a **Client ID** and **Client Secret**. These are required for accessing the API. - -## 2. Store Your Credentials - -Save the **Client ID** and **Client Secret** in a `.sh` file (e.g., `-env.sh`) within a `key` directory. The file should look like this: - -```bash -CLIENT_ID=YOUR_CLIENT_ID -CLIENT_SECRET=YOUR_SECRET_KEY -``` - -Set also these keys with following values: -```bash -AUTH_URI=https://accounts.google.com/o/oauth2/auth -TOKEN_URI=https://oauth2.googleapis.com/token -``` -If you get problems, most likely you will need to change **AUTH_URI** or **TOKEN_URI** to the appropriate one. Try to download your API KEY that you created in JSON format. Then open it and you will see right links. Just copy them and past to file. -Otherwise, follow [Google OAuth Documentation](https://developers.google.com/identity/protocols/oauth2/) to solve them. -Most likely you will need to change **AUTH_URI** or **TOKEN_URI** to the appropriate one. - -## How to Use in Shell - -To apply these variables to your current shell session, use: - -```bash -. ./key/-env.sh -``` - -This command sources the script, making the variables available in your current session. Ensure `-env.sh` is in the `key` directory relative to your current location. \ No newline at end of file diff --git a/module/move/gspread/.secret/readme.md b/module/move/gspread/.secret/readme.md new file mode 100644 index 0000000000..5306cf8644 --- /dev/null +++ b/module/move/gspread/.secret/readme.md @@ -0,0 +1,39 @@ +# Getting API Keys for OAuth Authentication + +Follow these steps to create and configure your OAuth credentials for using Google APIs. + +## 1. Create API Credentials + +1. Go to the [Google API Console](https://console.developers.google.com/). +2. From the projects list, select an existing project or create a new one. +3. In the left side menu, select **APIs & Services**. +4. On the left menu, click **Credentials**. +5. Click **Create Credentials** and select **OAuth client ID**. +6. In the **Application type** section, select **Desktop app**. +7. Provide an appropriate name for your client ID (e.g., "Gspread OAuth Client"). +8. Click **Create**. + +Once the credential is created, you will receive a **Client ID** and **Client Secret**. These are required for accessing the API. + +## 2. Store Your Credentials + +Save the **Client ID** and **Client Secret** in a `.env` within a `.secret` directory. The file should look like this: + +```bash +CLIENT_ID=YOUR_CLIENT_ID +CLIENT_SECRET=YOUR_SECRET_KEY +``` + + +# Troubleshooting + +If you encounter problems with authentication or tokens, you will most likely need to add **AUTH_URI** or **TOKEN_URI** to the .env file. To retrieve them, download the API key you created in JSON format. Open the file and copy the keys into the .env file. After making these changes, your .env file should look like this: + +```bash +CLIENT_ID=YOUR_CLIENT_ID +CLIENT_SECRET=YOUR_SECRET_KEY +AUTH_URI=YOUR_AUTH_URI +TOKEN_URI=YOUR_TOKEN_URI +``` + +If you still get some issues, follow [Google OAuth Documentation](https://developers.google.com/identity/protocols/oauth2/). \ No newline at end of file diff --git a/module/move/gspread/src/bin/main.rs b/module/move/gspread/src/bin/main.rs index 71fd85c041..08883dbd95 100644 --- a/module/move/gspread/src/bin/main.rs +++ b/module/move/gspread/src/bin/main.rs @@ -15,7 +15,7 @@ async fn main() -> Result< (), Box< dyn Error > > { dotenv().ok(); - let secret = Secret::load()?; + let secret = Secret::read(); let hub = hub( &secret ).await?; diff --git a/module/move/gspread/src/secret.rs b/module/move/gspread/src/secret.rs index f70792b958..48567b77f4 100644 --- a/module/move/gspread/src/secret.rs +++ b/module/move/gspread/src/secret.rs @@ -52,7 +52,7 @@ mod private #[ allow( non_snake_case ) ] pub fn load() -> Result< Self > { - let path = "./.key/-env.sh"; + let path = "./.secret/.env"; let r = dotenv::from_path( path ); if let Err( ref err ) = r @@ -67,8 +67,8 @@ mod private { CLIENT_SECRET : var( "CLIENT_SECRET", None )?, CLIENT_ID : var( "CLIENT_ID", None )?, - AUTH_URI : var ( "AUTH_URI", None )?, - TOKEN_URI : var ( "TOKEN_URI", None )? + AUTH_URI : var ( "AUTH_URI", Some( "https://accounts.google.com/o/oauth2/auth" ) )?, + TOKEN_URI : var ( "TOKEN_URI", Some( "https://oauth2.googleapis.com/token" ) )? }; Ok( config ) } @@ -77,7 +77,7 @@ mod private { Self::load().unwrap_or_else( | err | { - let example = include_str!("../.key/readme.md"); + let example = include_str!("../.secret/readme.md"); let explanation = format! ( r#" = Lack of secrets @@ -87,7 +87,7 @@ Failed to load secret or some its parameters. = Fix -Either define missing environment variable or make sure `./.key/-env.toml` file has it defined. +Add missing secret to .env file in .secret directory. Example: MISSING_SECRET=YOUR_MISSING_SECRET = More information @@ -109,10 +109,10 @@ Either define missing environment variable or make sure `./.key/-env.toml` file fn var ( name : &'static str, - default : Option<&'static str>, + default : Option< &'static str >, ) -> Result < String > { - match env::var(name) + match env::var( name ) { Ok( val ) => Ok ( val ), Err( _ ) => From cea45de5c39b2958e69d41eba0be62382eb0594b Mon Sep 17 00:00:00 2001 From: Vsevolod Date: Wed, 4 Dec 2024 23:55:16 +0100 Subject: [PATCH 2/5] readme file fixed --- module/move/gspread/.secret/readme.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/module/move/gspread/.secret/readme.md b/module/move/gspread/.secret/readme.md index 5306cf8644..2f0697485f 100644 --- a/module/move/gspread/.secret/readme.md +++ b/module/move/gspread/.secret/readme.md @@ -24,10 +24,12 @@ CLIENT_ID=YOUR_CLIENT_ID CLIENT_SECRET=YOUR_SECRET_KEY ``` +In most cases, only these two secrets are required. + # Troubleshooting -If you encounter problems with authentication or tokens, you will most likely need to add **AUTH_URI** or **TOKEN_URI** to the .env file. To retrieve them, download the API key you created in JSON format. Open the file and copy the keys into the .env file. After making these changes, your .env file should look like this: +If you encounter problems with authentication or tokens, you will most likely need to add **AUTH_URI** or **TOKEN_URI** to the .env file. In such case all 4 secrets are requeired. To retrieve them, download the API key you created in JSON format. Open the file and copy the keys into the .env file. After making these changes, your .env file should look like this: ```bash CLIENT_ID=YOUR_CLIENT_ID From f30e3cbb9a06ddd0b47ab779373beb79d33217d1 Mon Sep 17 00:00:00 2001 From: Vsevolod Date: Tue, 10 Dec 2024 13:33:51 +0100 Subject: [PATCH 3/5] secrets description changed --- module/move/gspread/.secret/readme.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/module/move/gspread/.secret/readme.md b/module/move/gspread/.secret/readme.md index 2f0697485f..e3e100f72d 100644 --- a/module/move/gspread/.secret/readme.md +++ b/module/move/gspread/.secret/readme.md @@ -24,12 +24,13 @@ CLIENT_ID=YOUR_CLIENT_ID CLIENT_SECRET=YOUR_SECRET_KEY ``` -In most cases, only these two secrets are required. +## 3. Why do we need it? +After executing each command, you need to grant the GSPREAD program access to the Google API. You will receive a link that begin with 'Please direct your browser to https://....' that will redirect you to your browser, where you must authorize the access. You will need to select the appropriate Google account that has the credentials for the application. The **CLIENT_ID** and **CLIENT_SECRET** are set up to do this process. -# Troubleshooting +## 4. Troubleshooting -If you encounter problems with authentication or tokens, you will most likely need to add **AUTH_URI** or **TOKEN_URI** to the .env file. In such case all 4 secrets are requeired. To retrieve them, download the API key you created in JSON format. Open the file and copy the keys into the .env file. After making these changes, your .env file should look like this: +If you encounter a page displaying an error instead of the Google account selection screen, it is likely that you need to add **AUTH_URI** or **TOKEN_URI** to the .env file. In this case, all four secrets are required. To retrieve them, download the API key you created in JSON format. Open the file and copy the necessary keys into the .env file. After making these changes, your .env file should look like this: ```bash CLIENT_ID=YOUR_CLIENT_ID From 57846259797b66a0b918f19eeb8498b88a77f455 Mon Sep 17 00:00:00 2001 From: Vsevolod Date: Tue, 10 Dec 2024 13:36:01 +0100 Subject: [PATCH 4/5] removed unused imports --- module/move/gspread/src/bin/main.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/module/move/gspread/src/bin/main.rs b/module/move/gspread/src/bin/main.rs index 08883dbd95..8f55f07f1c 100644 --- a/module/move/gspread/src/bin/main.rs +++ b/module/move/gspread/src/bin/main.rs @@ -1,5 +1,4 @@ use std::error::Error; -use std::env; use clap::Parser; use dotenv::dotenv; From d09fb29fecd25625e1f585d4796fc6160b773011 Mon Sep 17 00:00:00 2001 From: Vsevolod Date: Tue, 10 Dec 2024 14:02:31 +0100 Subject: [PATCH 5/5] fixed: wrong url error handling --- module/move/gspread/src/actions/gspread.rs | 17 ++++++++++++---- .../move/gspread/src/commands/gspread_cell.rs | 20 +++++++++++++++++-- .../gspread/src/commands/gspread_cells.rs | 10 +++++++++- .../gspread/src/commands/gspread_header.rs | 11 +++++++++- .../move/gspread/src/commands/gspread_rows.rs | 10 +++++++++- 5 files changed, 59 insertions(+), 9 deletions(-) diff --git a/module/move/gspread/src/actions/gspread.rs b/module/move/gspread/src/actions/gspread.rs index 3bee6a3e27..60b0fd980c 100644 --- a/module/move/gspread/src/actions/gspread.rs +++ b/module/move/gspread/src/actions/gspread.rs @@ -23,13 +23,19 @@ mod private #[ from ] #[ serde_as( as = "DisplayFromStr" ) ] google_sheets4::Error - ) + ), + + #[ error( "Invalid URL format: {0}" ) ] + InvalidUrl + ( + String + ), } pub fn get_spreadsheet_id_from_url ( url : &str - ) -> Option< &str > + ) -> Result< &str > { let re = Regex::new( r"d/([^/]+)/edit" ).unwrap(); @@ -37,11 +43,14 @@ mod private { if let Some( id ) = captures.get( 1 ) { - return Some( id.as_str() ); + return Ok( id.as_str() ); } } - None + Err + ( + Error::InvalidUrl( "Wrong url format.\nFix: copy sheet's the whole url from your browser. Usage: --url ''".to_string() ) + ) } pub type Result< T > = core::result::Result< T, Error >; diff --git a/module/move/gspread/src/commands/gspread_cell.rs b/module/move/gspread/src/commands/gspread_cell.rs index e81f9d8595..057da2dd09 100644 --- a/module/move/gspread/src/commands/gspread_cell.rs +++ b/module/move/gspread/src/commands/gspread_cell.rs @@ -55,7 +55,15 @@ mod private { Commands::Get { url, tab, cel } => { - let spreadsheet_id = get_spreadsheet_id_from_url( url.as_str() ).unwrap(); + let spreadsheet_id = match get_spreadsheet_id_from_url( url.as_str() ) + { + Ok( id ) => id, + Err( error ) => + { + eprintln!( "Error extracting spreadsheet ID: {}", error ); + return; + } + }; let result = actions::gspread_cell_get::action ( @@ -74,7 +82,15 @@ mod private Commands::Set { url, tab, cel, val } => { - let spreadsheet_id = get_spreadsheet_id_from_url( url.as_str() ).unwrap(); + let spreadsheet_id = match get_spreadsheet_id_from_url( url.as_str() ) + { + Ok( id ) => id, + Err( error ) => + { + eprintln!( "Error extracting spreadsheet ID: {}", error ); + return; + } + }; let result = actions::gspread_cell_set::action ( diff --git a/module/move/gspread/src/commands/gspread_cells.rs b/module/move/gspread/src/commands/gspread_cells.rs index cd7a4cc555..13ecf1e378 100644 --- a/module/move/gspread/src/commands/gspread_cells.rs +++ b/module/move/gspread/src/commands/gspread_cells.rs @@ -41,7 +41,15 @@ mod private { Commands::Set { select_row_by_key, json, url, tab } => { - let spreadsheet_id = get_spreadsheet_id_from_url( url.as_str() ).unwrap(); + let spreadsheet_id = match get_spreadsheet_id_from_url( url.as_str() ) + { + Ok( id ) => id, + Err( error ) => + { + eprintln!( "Error extracting spreadsheet ID: {}", error ); + return; + } + }; let result = actions::gspread_cells_set::action ( diff --git a/module/move/gspread/src/commands/gspread_header.rs b/module/move/gspread/src/commands/gspread_header.rs index 49be5e6e86..5048d3e4ed 100644 --- a/module/move/gspread/src/commands/gspread_header.rs +++ b/module/move/gspread/src/commands/gspread_header.rs @@ -41,7 +41,16 @@ mod private { CommonArgs { url, tab } => { - let spreadsheet_id = get_spreadsheet_id_from_url( url.as_str() ).unwrap(); + let spreadsheet_id = match get_spreadsheet_id_from_url( url.as_str() ) + { + Ok( id ) => id, + Err( error ) => + { + eprintln!( "Error extracting spreadsheet ID: {}", error ); + return; + } + }; + let result = actions::gspread_get_header::action ( hub, diff --git a/module/move/gspread/src/commands/gspread_rows.rs b/module/move/gspread/src/commands/gspread_rows.rs index 86f0c41f59..426d7f2dde 100644 --- a/module/move/gspread/src/commands/gspread_rows.rs +++ b/module/move/gspread/src/commands/gspread_rows.rs @@ -40,7 +40,15 @@ mod private { CommonArgs { url, tab } => { - let spreadsheet_id = get_spreadsheet_id_from_url( url.as_str() ).unwrap(); + let spreadsheet_id = match get_spreadsheet_id_from_url( url.as_str() ) + { + Ok( id ) => id, + Err( error ) => + { + eprintln!( "Error extracting spreadsheet ID: {}", error ); + return; + } + }; let result = actions::gspread_get_rows::action (