aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
authorTeddy Wing2021-05-18 23:05:22 +0200
committerTeddy Wing2021-05-18 23:12:43 +0200
commitf55502b7b9aad79b1fdf810429598b460841b41f (patch)
tree9c8dadaa164881acdb50e3a0260fdd2615b89c55 /src/main.rs
parentd55070f102c3b59a30248e9e0f87cf91365fe240 (diff)
downloadgoogle-calendar-rsvp-f55502b7b9aad79b1fdf810429598b460841b41f.tar.bz2
Get a calendar event from Google Calendar
Try out the `calendar3` API to get a single event from Google Calendar. Mostly copied the example code from the `google-calendar3` documentation: https://docs.rs/google-calendar3/2.0.4+20210327/google_calendar3/index.html#a-complete-example Modified it to get a single event, and to use the OAuth2 secret from `~/.google-service-cli/calendar3-secret.json`.
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs65
1 files changed, 63 insertions, 2 deletions
diff --git a/src/main.rs b/src/main.rs
index e7a11a9..9bd94bb 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,3 +1,64 @@
-fn main() {
- println!("Hello, world!");
+use google_calendar3::{CalendarHub, Result, Error};
+use home;
+use hyper;
+use hyper_rustls;
+use tokio;
+use yup_oauth2 as oauth2;
+
+use std::fs;
+
+
+#[tokio::main]
+async fn main() {
+ // let secret: oauth2::ApplicationSecret = Default::default();
+ let secret = secret_from_file();
+
+ let auth = oauth2::InstalledFlowAuthenticator::builder(
+ secret,
+ oauth2::InstalledFlowReturnMethod::HTTPRedirect,
+ ).build().await.unwrap();
+
+ let hub = CalendarHub::new(
+ hyper::Client::builder()
+ .build(hyper_rustls::HttpsConnector::with_native_roots()),
+ auth,
+ );
+
+ let result = hub.events()
+ .get("primary", "1uj192r9qtotb6se0rov83ahr6")
+ .doit()
+ .await;
+
+ match result {
+ Err(e) => match e {
+ // The Error enum provides details about what exactly happened.
+ // You can also just use its `Debug`, `Display` or `Error` traits
+ Error::HttpError(_)
+ |Error::Io(_)
+ |Error::MissingAPIKey
+ |Error::MissingToken(_)
+ |Error::Cancelled
+ |Error::UploadSizeLimitExceeded(_, _)
+ |Error::Failure(_)
+ |Error::BadRequest(_)
+ |Error::FieldClash(_)
+ |Error::JsonDecodeError(_, _) => println!("{}", e),
+ },
+ Ok(res) => println!("Success: {:?}", res),
+ }
+}
+
+fn secret_from_file() -> oauth2::ApplicationSecret {
+ let f = fs::File::open(
+ home::home_dir()
+ .unwrap()
+ .join(".google-service-cli/calendar3-secret.json"),
+ ).unwrap();
+
+ let console_secret: oauth2::ConsoleApplicationSecret = serde_json::from_reader(f).unwrap();
+
+ match console_secret.installed {
+ Some(secret) => secret,
+ None => todo!(),
+ }
}