diff --git a/README.md b/README.md index f42f0c0..a167d42 100644 --- a/README.md +++ b/README.md @@ -69,55 +69,14 @@ List events gcal list ``` -## Google Calendar API Authentication with OAuth2 - -This guide will help you set up OAuth2 authentication for your Google Calendar API. Follow these steps to create a project, enable the API, and obtain the necessary credentials. - -### Step 1: Create a New Project -1. Go to the [Google Developer Console](https://console.developers.google.com/). -2. Click on the **Create Project** button. -3. Enter a name for your project and click **Create**. - -### Step 2: Enable the Google Calendar API -1. In the [Google Developer Console](https://console.developers.google.com/), navigate to the **Library** section. -2. Search for "Google Calendar API". -3. Click on the **Google Calendar API** and then click **Enable**. - -### Step 3: Create OAuth2 Consent Screen -1. In the [Google Developer Console](https://console.developers.google.com/), navigate to the **OAuth consent screen** section. -2. Choose **External** as the user type and click **Create**. -3. Fill out the required app information: - - **App name**: `gcalcli` - - **User support email**: `your@email.com` -4. Fill out the required developer contact information: - - **Email addresses**: `your@email.com` -5. Click **Save and continue**. -6. Under **Scopes**, click **Save and continue**. -7. Under **Test users**, add your email (`your@gmail.com`). -8. Click **Save and continue**. - -### Step 4: Create OAuth Client ID -1. In the [Google Developer Console](https://console.developers.google.com/), navigate to the **Credentials** section. -2. Click **Create credentials** and select **OAuth client ID**. -3. Select **Application type: Desktop app**. -4. Click **Create**. -5. Download the JSON file containing your client ID and secret. - -### Step 5: Configure gcalcli -1. Create a directory for gcalcli configuration: - ```sh - mkdir -p ~/.gcal - ``` -2. Place the downloaded JSON file into the `~/.gcal` directory: - ```sh - mv /path/to/your/downloaded/secret.json ~/.gcal/secret.json - ``` - -### Step 6: Authenticate with Google + +## Authentication + +By default, a hardcoded and unverified Google secret is included in the project. This secret has a user cap limit and can be used temporarily. If you want to use your custom authentication (via Google Console), you can find step-by-step instructions [here](docs/custom_auth.md). + +How it works in the background ? + 1. Start gcalcli. The authentication process will begin automatically. 2. Follow the instructions to complete the authentication process. The resulting token will be stored in the `~/.gcal/store.json` file. - -By following these steps, you will have successfully set up OAuth2 authentication for your Google Calendar API using gcalcli. - diff --git a/docs/custom_auth.md b/docs/custom_auth.md new file mode 100644 index 0000000..32650d0 --- /dev/null +++ b/docs/custom_auth.md @@ -0,0 +1,43 @@ +## Google Calendar API Authentication with OAuth2 + +This guide will help you set up OAuth2 authentication for your Google Calendar API. Follow these steps to create a project, enable the API, and obtain the necessary credentials. + +### Step 1: Create a New Project +1. Go to the [Google Developer Console](https://console.developers.google.com/). +2. Click on the **Create Project** button. +3. Enter a name for your project and click **Create**. + +### Step 2: Enable the Google Calendar API +1. In the [Google Developer Console](https://console.developers.google.com/), navigate to the **Library** section. +2. Search for "Google Calendar API". +3. Click on the **Google Calendar API** and then click **Enable**. + +### Step 3: Create OAuth2 Consent Screen +1. In the [Google Developer Console](https://console.developers.google.com/), navigate to the **OAuth consent screen** section. +2. Choose **External** as the user type and click **Create**. +3. Fill out the required app information: + - **App name**: `gcalcli` + - **User support email**: `your@email.com` +4. Fill out the required developer contact information: + - **Email addresses**: `your@email.com` +5. Click **Save and continue**. +6. Under **Scopes**, click **Save and continue**. +7. Under **Test users**, add your email (`your@gmail.com`). +8. Click **Save and continue**. + +### Step 4: Create OAuth Client ID +1. In the [Google Developer Console](https://console.developers.google.com/), navigate to the **Credentials** section. +2. Click **Create credentials** and select **OAuth client ID**. +3. Select **Application type: Desktop app**. +4. Click **Create**. +5. Download the JSON file containing your client ID and secret. + +### Step 5: Configure gcalcli +1. Create a directory for gcalcli configuration: + ```sh + mkdir -p ~/.gcal + ``` +2. Place the downloaded JSON file into the `~/.gcal` directory: + ```sh + mv /path/to/your/downloaded/secret.json ~/.gcal/secret.json + ``` \ No newline at end of file diff --git a/src/util/calendar.rs b/src/util/calendar.rs index e89504e..0f6a8cc 100644 --- a/src/util/calendar.rs +++ b/src/util/calendar.rs @@ -28,16 +28,39 @@ pub async fn auth() -> Result>, Box oauth2::InstalledFlowAuthenticator::builder( + secret, + oauth2::InstalledFlowReturnMethod::HTTPRedirect, + ), + Err(_) => { + let secret: oauth2::ApplicationSecret = ApplicationSecret { + auth_uri: "https://accounts.google.com/o/oauth2/auth".to_string(), + client_secret: "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".to_string(), + token_uri: "https://accounts.google.com/o/oauth2/token".to_string(), + redirect_uris: vec!["urn:ietf:wg:oauth:2.0:oob".to_string()], + client_id: + "602236549045-3gcv7m50sp1d6vvqklimb5oaasp9ihi9.apps.googleusercontent.com" + .to_string(), + auth_provider_x509_cert_url: Some( + "https://www.googleapis.com/oauth2/v1/certs".to_string(), + ), + project_id: None, + client_email: None, + client_x509_cert_url: None, + }; + oauth2::InstalledFlowAuthenticator::builder( + secret, + oauth2::InstalledFlowReturnMethod::HTTPRedirect, + ) + } + }; let store_path = file::get_absolute_path(".gcal/store.json")?; - let auth = oauth2::InstalledFlowAuthenticator::builder( - secret, - oauth2::InstalledFlowReturnMethod::HTTPRedirect, - ) - .persist_tokens_to_disk(&store_path) - .build() - .await?; + let auth = auth_builder + .persist_tokens_to_disk(&store_path) + .build() + .await?; let scopes = &[ "https://www.googleapis.com/auth/calendar", @@ -47,7 +70,7 @@ pub async fn auth() -> Result>, Box {}, + Ok(_) => {} Err(e) => println!("Authentication error: {:?}", e), } @@ -63,12 +86,12 @@ pub async fn auth() -> Result>, Box>) -> Result { let result = hub.settings().list().doit().await; let settings = result.unwrap().1.items.unwrap_or_default(); - let timezone_setting = settings.iter() + let timezone_setting = settings + .iter() .find(|setting| setting.id == Some("timezone".to_string())) .ok_or("Timezone setting not found"); @@ -97,6 +120,11 @@ pub async fn get_default_timezone(hub: &CalendarHub Result { let secret = oauth2::read_application_secret(path) .await - .with_context(|| format!("Failed to read the Google application secret file from path {:?}.", path))?; + .with_context(|| { + format!( + "Failed to read the Google application secret file from path {:?}.", + path + ) + })?; Ok(secret) }