From bdb0bd92e7bcbecd0b4ce96467a7286f507526e2 Mon Sep 17 00:00:00 2001 From: Lewin Bormann Date: Thu, 13 Jun 2019 15:32:48 +0200 Subject: [PATCH] fix(examples): Update examples to use Authenticator. --- examples/test-device/src/main.rs | 17 ++++++++++++----- examples/test-installed/src/main.rs | 17 ++++++++++++----- examples/test-svc-acct/src/main.rs | 9 ++++++++- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/examples/test-device/src/main.rs b/examples/test-device/src/main.rs index d5f8b07..f24068e 100644 --- a/examples/test-device/src/main.rs +++ b/examples/test-device/src/main.rs @@ -1,5 +1,5 @@ use futures::prelude::*; -use yup_oauth2::{self, GetToken}; +use yup_oauth2::{self, Authenticator, GetToken}; use hyper::client::Client; use hyper_tls::HttpsConnector; @@ -15,12 +15,19 @@ fn main() { let scopes = &["https://www.googleapis.com/auth/youtube.readonly".to_string()]; - let ad = yup_oauth2::DefaultAuthenticatorDelegate; - let mut df = yup_oauth2::DeviceFlow::new::(client, creds, ad, None); + let ad = yup_oauth2::DefaultFlowDelegate; + let mut df = yup_oauth2::DeviceFlow::new::(client.clone(), creds, ad, None); df.set_wait_duration(Duration::from_secs(120)); - let mut rt = tokio::runtime::Runtime::new().unwrap(); + let mut auth = Authenticator::new_disk( + client, + df, + yup_oauth2::DefaultAuthenticatorDelegate, + "tokenstorage.json", + ) + .expect("authenticator"); - let fut = df + let mut rt = tokio::runtime::Runtime::new().unwrap(); + let fut = auth .token(scopes.iter()) .and_then(|tok| Ok(println!("{:?}", tok))); diff --git a/examples/test-installed/src/main.rs b/examples/test-installed/src/main.rs index 47a8215..18a2440 100644 --- a/examples/test-installed/src/main.rs +++ b/examples/test-installed/src/main.rs @@ -1,6 +1,6 @@ use futures::prelude::*; use yup_oauth2::GetToken; -use yup_oauth2::InstalledFlow; +use yup_oauth2::{Authenticator, InstalledFlow}; use hyper::client::Client; use hyper_tls::HttpsConnector; @@ -10,19 +10,26 @@ use std::path::Path; fn main() { let https = HttpsConnector::new(1).expect("tls"); let client = Client::builder().build::<_, hyper::Body>(https); - let ad = yup_oauth2::DefaultAuthenticatorDelegate; + let ad = yup_oauth2::DefaultFlowDelegate; let secret = yup_oauth2::read_application_secret(Path::new("clientsecret.json")) .expect("clientsecret.json"); - let mut inf = InstalledFlow::new( - client, + let inf = InstalledFlow::new( + client.clone(), ad, secret, yup_oauth2::InstalledFlowReturnMethod::HTTPRedirect(8081), ); + let mut auth = Authenticator::new_disk( + client, + inf, + yup_oauth2::DefaultAuthenticatorDelegate, + "tokencache.json", + ) + .unwrap(); let s = "https://www.googleapis.com/auth/drive.file".to_string(); let scopes = vec![s]; - let tok = inf.token(scopes.iter()); + let tok = auth.token(scopes.iter()); let fut = tok.map_err(|e| println!("error: {:?}", e)).and_then(|t| { println!("The token is {:?}", t); Ok(()) diff --git a/examples/test-svc-acct/src/main.rs b/examples/test-svc-acct/src/main.rs index 88a1eda..b3fd150 100644 --- a/examples/test-svc-acct/src/main.rs +++ b/examples/test-svc-acct/src/main.rs @@ -23,6 +23,13 @@ fn main() { println!("token is: {:?}", tok); Ok(()) }); + let fut2 = sa + .token(["https://www.googleapis.com/auth/pubsub"].iter()) + .and_then(|tok| { + println!("cached token is {:?} and should be identical", tok); + Ok(()) + }); + let all = fut.join(fut2); let mut rt = tokio::runtime::Runtime::new().unwrap(); - rt.block_on(fut).unwrap() + rt.block_on(all).unwrap(); }